[英]How can I calculate the percentage of empty values in a pandas dataframe?
I have a dataframe df
, from which I know there are empty values, ie '' (blank spaces).我有一个 dataframe
df
,从中我知道有空值,即 '' (空格)。 I want to calculate the percentage per column of those observations and replace them with NaN
.我想计算这些观察值每列的百分比并将它们替换为
NaN
。
To get the percentage I've tried:要获得我尝试过的百分比:
for col in df:
empty = round((df[df[col]] == '').sum()/df.shape[0]*100, 1)
I have a similar code which calculates the zeros, which does work:我有一个类似的代码来计算零点,它确实有效:
zeros = round((df[col] == 0).sum()/df.shape[0]*100, 1)
I think you need Series.isna
for test missing values (but not empty spaces):我认为您需要
Series.isna
来测试缺失值(但不是空格):
nans = round(df[col].isna().sum()/df.shape[0]*100, 1)
Solution should be simplify with mean
:解决方案应简化为
mean
:
nans = round(df[col].isna().mean()*100, 1)
For replace empty spaces or spaces to NaN
s use:要将空格或空格替换为
NaN
,请使用:
df = df.replace(r'^\s*$', np.nan, regex=True)
nans = round(df[col].isna().mean()*100, 1)
If need test all columns:如果需要测试所有列:
nans = df.isna().mean().mul(100).round()
The full answer to your problem will be:您的问题的完整答案将是:
for col in df:
empty_avg = round(df[col].isna().mean()*100, 1) # This line is to find the average of empty values.
df = df[df != ''] # This will replace all the empty values with NaN.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.