根据另一列在数据框中删除一列

时间:2019-03-12 04:12:32

标签: python pandas dataframe

我有一个名为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
..... 

我还有一个称为“首选项”的数据框

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

我想从偏好值小于2的“职位”数据框中删除列,以便拥有


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

这就是我所拥有的

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

,但似乎没有删除(学位和行业)列。任何帮助将不胜感激

2 个答案:

答案 0 :(得分:1)

我想您的尝试几乎在那里。这是我所拥有的:

>>>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

答案 1 :(得分:0)

这应该对您有用:

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

这就是为什么您的代码行不起作用的原因:

  • 首先,缺少右括号(但是我想那只是一个错字)
  • filter方法应应用于preferences,而不是jobs
  • filter并不是您真正想要在此处使用的名称列表:preferences.loc[preferences.value < 2, 'name']返回所有值为<2
  • 的名称列表
相关问题