Python Pandas数据框发现缺失值 [英] Python Pandas dataframe find missing values
问题描述
我正在尝试查找缺失值,然后删除缺失值.试图在线查找数据,但似乎找不到答案.
I'm trying to find missing values and then drop off missing values. Tried looking for the data online but can't seem to find the answer.
提取的数据框:
在df中,对于1981年和1982年,该值应为-",即缺少值.我想找到丢失的值,然后删除丢失的值.
In the df, for 1981 and 1982, it should be '-', i.e. missing values. I would like to find the missing values then drop off the missing values.
使用isull导出的数据框:
我使用了df.isnull(),但是在1981年和1982年,它被检测为假",这意味着有数据.但是它应该是-",因此被认为是缺失值.
I used df.isnull() but in 1981 and 1982, it's detected as 'False' which means there's data. But it should be '-', therefore considered as missing values.
我在下面粘贴了我的代码.我错过了什么?
I had pasted my code below. What am I missing out?
import pandas as pd
mydf = pd.read_excel('abc.xlsx', sep='\t')
df1 = mydf.set_index('Variables')
df = df1[0:10]
print(df)
print(df.isnull())
推荐答案
问题有两点:查找哪些列缺少值并删除这些值.
The question has two points: finding which columns have missing values and drop those values.
要查找数据框 df
missing = df.isnull().sum()
print(missing)
要除去那些遗漏的值(除了@jezrael的考虑之外),如果这没有帮助,我建议您使用
To drop those missing values, apart from @jezrael's consideration, if that doesn't help, I suggest you to use dropna
:
删除所有缺少元素的行.
Drop the rows where all elements are missing.
df.dropna(how='all')
删除至少缺少一个元素的列.
Drop the columns where at least one element is missing.
df.dropna(axis='columns')
这篇关于Python Pandas数据框发现缺失值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!