[英]Dropping a column in a dataframe based on another column
I have a dataframe called jobs我有一个名为 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我想从“jobs”数据框中删除偏好值小于 2 的列,以便我有
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:这就是您的代码行不起作用的原因:
filter
method should be applied to preferences
instead of jobs
filter
方法应该应用于preferences
而不是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 filter
并不是你真正想在这里用来获取名称列表的东西: preferences.loc[preferences.value < 2, 'name']
返回值 < 2 的所有名称的列表
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.