Python Pandas 数据框查找缺失值

Python Pandas dataframe find missing values(Python Pandas 数据框查找缺失值)

本文介绍了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 数据框查找缺失值

基础教程推荐