简体   繁体   English

基于另一列删除数据框中的列

[英]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:这就是您的代码行不起作用的原因:

  • 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方法应该应用于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.

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