Python Newbie
Python Newbie

Reputation: 127

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

Upvotes: 1

Views: 598

Answers (2)

kerwei
kerwei

Reputation: 1842

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

Upvotes: 1

mommermi
mommermi

Reputation: 1052

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

Upvotes: 0

Related Questions