简体   繁体   中英

pandas:numeric columns fillna with mean and character columns fillna with mode

我知道如何选择所有数字列和fillna与平均值,但如何使用模式的平均和字符列fillna数字列fillna?

Use select_dtypes for numeric columns with mean , then get non numeric with difference and mode , join together by append and last call fillna :

Notice: (thanks @jpp)

Function mode should return multiple values, for seelct first add iloc

df = pd.DataFrame({
        'A':list('ebcded'),
         'B':[np.nan,np.nan,4,5,5,4],
         'C':[7,np.nan,9,4,2,3],
         'D':[1,3,5,np.nan,1,0],
         'F':list('aaabbb')
})

df.loc[[0,1], 'F'] = np.nan
df.loc[[2,1], 'A'] = np.nan
print (df)
     A    B    C    D    F
0    e  NaN  7.0  1.0  NaN
1  NaN  NaN  NaN  3.0  NaN
2  NaN  4.0  9.0  5.0    a
3    d  5.0  4.0  NaN    b
4    e  5.0  2.0  1.0    b
5    d  4.0  3.0  0.0    b

a = df.select_dtypes(np.number).mean()
b = df[df.columns.difference(a.index)].mode().iloc[0]
#alternative
#b = df.select_dtypes(object).mode().iloc[0]

print (df[df.columns.difference(a.index)].mode())
   A    F
0  d    b
1  e  NaN

df = df.fillna(a.append(b))
print (df)
   A    B    C    D  F
0  e  4.5  7.0  1.0  b
1  d  4.5  5.0  3.0  b
2  d  4.0  9.0  5.0  a
3  d  5.0  4.0  2.0  b
4  e  5.0  2.0  1.0  b
5  d  4.0  3.0  0.0  b

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM