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

huangapple go评论89阅读模式
英文:

How I can delete a specific variable names in Python?

问题

这是数据:

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) 

但它并没有删除所有的 "Not Reported" 行。有没有办法删除数据集中的 "Not Reported" 行?

英文:

Here's the data:

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

I wrote

df.dropna(inplace=True) 

but it did not work for all "Not Reported". Is there any ways to delete "not reported" rows in the dataset?

答案1

得分: 1

你可以使用以下代码删除数据集中的 Not Reported 值所在的行:

df = df[df != 'Not Reported'].dropna(how='any')
英文:

You can delete rows with Not Reported values in your dataset using the following code:

df = df[df != 'Not Reported'].dropna(how='any')

答案2

得分: 0

Not Reported转换为None

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

Convert Not Reported to None

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

huangapple
  • 本文由 发表于 2023年6月15日 04:17:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76477243.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定