如何在 Python 中删除特定的变量名称?

How I can delete a specific variable names in Python?

提问人:nur 提问时间:6/15/2023 最后编辑:Barmarnur 更新时间:6/15/2023 访问量:63

问:

数据如下:

id  date    age case_id country province    region  sex travel  travelh
1   1516    2020-03-23  40-49   1516    Canada  Ontario York    Male    1   Costa Rica
4   5658    2020-03-29  60-69   5657    Canada  Ontario Algoma  Female  1   United States
5   642 2020-03-18  Not Reported    642 Canada  Nova Scotia Not Reported    Not Reported    1   Not Reported
6   3560    2020-03-26  60-69   3560    Canada  Ontario Simcoe Muskoka  Male    1   India, United Arab Emirates
7   725 2020-03-18  80-89   725 Canada  Ontario Chatham-Kent    Female  1   United States

我写了

df.dropna(inplace=True) 

但它并不适用于所有“未报告”。有什么方法可以删除数据集中的“未报告”行?

python pandas 变量 del

评论

1赞 Barmar 6/15/2023
dropna()删除值。 与 不同。NaNNot ReportedNaN
0赞 user19077881 6/15/2023
据推测,“未报告”是一个字符串;然后你可以使用df = df.replace('Not Reported', np.nan).dropna()
0赞 Goku - stands with Palestine 6/15/2023
是否要全部替换为“NA”或“0”?Not Reported

答:

1赞 Abdul Moeez 6/15/2023 #1

您可以使用以下代码删除数据集中具有值的行:Not Reported

df = df[df != 'Not Reported'].dropna(how='any')
0赞 Stu Sztukowski 6/15/2023 #2

转换为Not ReportedNone

df = df.replace('Not Reported', None).dropna()