Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python: filter dataframe rows so that values are in columns of other dataframe

Consider that I have one dataframe that looks like this:

Gender Employed
1      1
0      0 
1      0
0      1

And I have another dataframe that looks like:

Name  Area
Andy  Gender
Ally  HR
Chris Employed
Tom   Food

I only want to keep the row entries in the area column that correspond to the column names of my first dataframe. These are example dataframes and my actual dataframe has hundreds of columns so no very specific answers involving ‘Gender’ and ‘Employed’ will work.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The end result should be

Name  Area
Andy  Gender
Chris Employed

>Solution :

You can filter a df like this:

df[df['Area'].isin(df2.columns)]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading