将pandas数据帧中有N个空值的列完全填充为零



取一条类似的线

df.dropna(thresh=2)

我不想根据空值的#来删除列,而是想用零来填充整列。

因此,只有一个空值的列将不受影响,而具有2个以上空值的行将完全替换为零。

在列上迭代,如果NA值的数量大于2,则用零替换列,否则保持列原样:

for col in df.columns:
df[col] = 0 if df[col].isna().sum() > 2 else df[col]

尝试这个

df=pd.DataFrame({"col1":[1,2,3,4,5,7],"col2":[1,2,3,4,np.NaN,2],"col3":[1,2,3,np.NaN,np.NaN,np.NaN]})
temp=(((df!=df).sum())>=2)#nothing much is happening here its simply just a filter for nan elements
temp1=df.columns
for x in range(len(temp)):
if temp[x]:
df[temp1[x]]=0
df

输出:

col1   col2    col3
0   1   1.0     0
1   2   2.0     0
2   3   3.0     0
3   4   4.0     0
4   5   NaN     0
5   7   2.0     0

最新更新