繁体   English   中英

Python数据框列删除

[英]Python dataframe column drop

如果我在状态列中有推荐的行,那么我想删除所有具有匹配 ID 的作业 ID,而不管状态如何。 例如,如果 Job ID 1234 有任何状态为推荐的员工,则将其从数据框中删除。 我想要一种方法来做到这一点,而不必在每一行中手动输入要从数据框中删除的作业 ID。 我目前这样做的方式是将作业 ID 手动输入到以下 Python 代码中:

x.drop(x[x['Job ID'] == '1234'].index, inplace = True)

截屏

import pandas as pd

data = [{'job_id': 1, 'status': 'Recommended'},
        {'job_id': 1, 'status': 'Not Recommended'},
        {'job_id': 2, 'status': 'Never Recommended'},
        {'job_id': 2, 'status': 'Not Recommended'},
        {'job_id': 3, 'status': 'Never Recommended'},
        {'job_id': 3, 'status': 'R U kidding?'},
        {'job_id': 3, 'status': 'No, for real...'},
        {'job_id': 4, 'status': 'Recommended'},
        {'job_id': 4, 'status': 'Not Recommended'}]

df = pd.DataFrame.from_records(data)

#Show the dataframe
print(df.to_string())

#Get the job_id of any row with a status of Recommended; there might be duplicates, so do a unique
ids_to_delete = df[df["status"] == "Recommended"]["job_id"].unique()

#Use the ~ operator to get the records that don't have an ID in the list generated in the prior step
df = df[~df["job_id"].isin(ids_to_delete)]

#Show the dataframe; any row that had a status of Recommended is now gone
print(df.to_string())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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