简体   繁体   中英

Dropping a column in a dataframe based on another column

I have a dataframe called jobs

position           software    salary  degree     location    industry
architect          autoCAD     400     masters    london       AEC
data analyst       python      500     bachelors   New York   Telecommunications
personal assistant excel       200     bachelors   London      Media
..... 

I have another dataframe called 'preference'

name         value
position      2
software      4
salary        3
degree        1
location      3
industry      1  

I'd like to drop columns from the 'jobs' dataframe whose preference value is less than 2 so that I have


position           software    salary     location    
architect          autoCAD     400        london       
data analyst       python      500        New York   
personal assistant excel       200        London      
..... 

This is what I have

jobs.drop(list(jobs.filter(preference['value'] < 2), axis = 1, inplace = True)

but it doesn't seem to drop the (degree and industry) columns. Any help would be appreciated

Your attempt is almost there I think. Here's what I have:

>>>jobs.drop(preference.loc[preference['value'] < 2,'name'], axis=1, inplace=True)

             position software  salary  location
0           architect  autoCAD     400    london
1        data analyst   python     500  New York
2  personal assistant    excel     200    London

This should work for you:

jobs.drop(preferences.loc[preferences.value < 2, 'name'], axis=1, inplace=True)

This is why your line of code did not work:

  • first of all, there is a closing parenthesis missing (but I guess that's just a typo)
  • the filter method should be applied to preferences instead of jobs
  • filter is not really what you want to use here to get a list of names: preferences.loc[preferences.value < 2, 'name'] returns a list of all names with value < 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM