oro*_*ome 25 reporting nan missing-data pandas
在R中,我可以使用该summary
命令快速查看缺失数据的计数,但是等效的pandas
DataFrame方法describe
不会报告这些值.
我知道我可以做点什么
len(mydata.index) - mydata.count()
Run Code Online (Sandbox Code Playgroud)
计算每列的缺失值的数量,但我想知道是否有更好的习语(或者我的方法是否正确).
Jef*_*eff 38
双方describe
并info
上报非缺失值的计数.
In [1]: df = DataFrame(np.random.randn(10,2))
In [2]: df.iloc[3:6,0] = np.nan
In [3]: df
Out[3]:
0 1
0 -0.560342 1.862640
1 -1.237742 0.596384
2 0.603539 -1.561594
3 NaN 3.018954
4 NaN -0.046759
5 NaN 0.480158
6 0.113200 -0.911159
7 0.990895 0.612990
8 0.668534 -0.701769
9 -0.607247 -0.489427
[10 rows x 2 columns]
In [4]: df.describe()
Out[4]:
0 1
count 7.000000 10.000000
mean -0.004166 0.286042
std 0.818586 1.363422
min -1.237742 -1.561594
25% -0.583795 -0.648684
50% 0.113200 0.216699
75% 0.636036 0.608839
max 0.990895 3.018954
[8 rows x 2 columns]
In [5]: df.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 10 entries, 0 to 9
Data columns (total 2 columns):
0 7 non-null float64
1 10 non-null float64
dtypes: float64(2)
Run Code Online (Sandbox Code Playgroud)
为了得到失踪的数量,你的解决方案是正确的
In [20]: len(df.index)-df.count()
Out[20]:
0 3
1 0
dtype: int64
Run Code Online (Sandbox Code Playgroud)
你也可以这样做
In [23]: df.isnull().sum()
Out[23]:
0 3
1 0
dtype: int64
Run Code Online (Sandbox Code Playgroud)
小智 8
下面的一个就可以解决这个问题,并返回每列的空值计数:
df.isnull().sum(axis=0)
df.isnull()
返回具有 True / False 值的数据框,
sum(axis=0)
对列中所有行的值进行求和
作为一个很小的补充,要获得DataFrame列丢失的百分比,将@Jeff和@userS的上述答案结合起来可以得到:
df.isnull().sum()/len(df)*100
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
46662 次 |
最近记录: |