[英]Dropping a column in a dataframe based on another column
我有一個名為 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
我想從“jobs”數據框中刪除偏好值小於 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)
但它似乎沒有刪除(學位和行業)列。 任何幫助,將不勝感激
我想你的嘗試幾乎就在那里。 這是我所擁有的:
>>>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
這應該適合你:
jobs.drop(preferences.loc[preferences.value < 2, 'name'], axis=1, inplace=True)
這就是您的代碼行不起作用的原因:
filter
方法應該應用於preferences
而不是jobs
filter
並不是你真正想在這里用來獲取名稱列表的東西: preferences.loc[preferences.value < 2, 'name']
返回值 < 2 的所有名稱的列表
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.