Python Pandas dataframe find missing values(Python Pandas 数据框查找缺失值)
问题描述
我正在尝试查找缺失值,然后删除缺失值.尝试在线查找数据,但似乎找不到答案.
提取的数据框:
在 df 中,对于 1981 年和 1982 年,它应该是 '-',即缺失值.我想找到缺失值,然后删除缺失值.
使用 isnull 导出的数据框:
我使用了 df.isnull() 但在 1981 年和 1982 年,它被检测为假",这意味着有数据.但它应该是'-',因此被认为是缺失值.
我在下面粘贴了我的代码.我错过了什么?
将 pandas 导入为 pdmydf = pd.read_excel('abc.xlsx', sep=' ')df1 = mydf.set_index('变量')df = df1[0:10]打印(df)打印(df.isnull())
问题有两点:找出哪些列有缺失值并删除那些值.
在数据帧上查找缺失值 df
缺失 = df.isnull().sum()打印(缺失)
要删除这些缺失值,除了@jezrael 的考虑之外,如果这没有帮助,我建议您使用 dropna
:
删除所有元素都缺失的行.
df.dropna(how='all')
<块引用>
删除至少缺少一个元素的列.
df.dropna(axis='columns')
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.
Extracted Dataframe:
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.
Exported Dataframe using isnull:
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=' ')
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.
To find the missing values on a dataframe df
missing = df.isnull().sum()
print(missing)
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 数据框查找缺失值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python Pandas 数据框查找缺失值


基础教程推荐
- 求两个直方图的卷积 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 包装空间模型 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01