pandas isnull sum 与列标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41681693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pandas isnull sum with column headers
提问by joshi123
I have a dataframe which has multiple columns. I'd like to iterate through the columns, counting for each column how many null values there are and produce a new dataframe which displays the sum of isnull
values alongside the column header names.
我有一个包含多列的数据框。我想遍历列,为每列计算有多少空值,并生成一个新的数据框,isnull
在列标题名称旁边显示值的总和。
If I do:
如果我做:
for col in main_df:
print(sum(pd.isnull(data[col])))
I get a list of the null count for each column:
我得到每列的空计数列表:
0
1
100
What I'm trying to do is create a new dataframe which has the column header alongside the null count, e.g.
我想要做的是创建一个新的数据框,它在空计数旁边有列标题,例如
col1 | 0
col2 | 1
col3 | 100
回答by MaxU
Try this:
尝试这个:
In [71]: df
Out[71]:
a b c
0 NaN 7.0 0
1 0.0 NaN 4
2 2.0 NaN 4
3 1.0 7.0 0
4 1.0 3.0 9
5 7.0 4.0 9
6 2.0 6.0 9
7 9.0 6.0 4
8 3.0 0.0 9
9 9.0 0.0 1
In [72]: pd.isnull(df).sum()
Out[72]:
a 1
b 2
c 0
dtype: int64
or:
或者:
In [76]: df.isnull().sum()
Out[76]:
a 1
b 2
c 0
dtype: int64
you can create a DF out of it:
你可以用它创建一个 DF:
In [78]: df.isnull().sum().to_frame('nulls')
Out[78]:
nulls
a 1
b 2
c 0
回答by Pierre Delecto
If the number of columns in your dataframe is greater than 10 you will end up with the middle columns being left out of the output. You can print every column using:
如果数据框中的列数大于 10,则最终将中间的列排除在输出之外。您可以使用以下方法打印每一列:
nulls = df.isnull().sum().to_frame()
for index, row in nulls.iterrows():
print(index, row[0])