Python/Pandas Dataframe用中值替换0

jea*_*elj 7 python mean median dataframe pandas

我有一个python pandas数据框,有几列,一列有0值.我想0用这个列的median或替换值mean.

data是我的数据框
artist_hotness是列

mean_artist_hotness = data['artist_hotness'].dropna().mean()

if len(data.artist_hotness[ data.artist_hotness.isnull() ]) > 0:
data.artist_hotness.loc[ (data.artist_hotness.isnull()), 'artist_hotness'] = mean_artist_hotness
Run Code Online (Sandbox Code Playgroud)

我尝试过这个,但它没有用.

jez*_*ael 10

我想你可以使用mask和添加的参数skipna=Truemean代替dropna。还需要将条件更改为data.artist_hotness == 0是否需要替换0值或data.artist_hotness.isnull()是否需要替换NaN值:

import pandas as pd
import numpy as np

data = pd.DataFrame({'artist_hotness': [0,1,5,np.nan]})
print (data)
   artist_hotness
0             0.0
1             1.0
2             5.0
3             NaN

mean_artist_hotness = data['artist_hotness'].mean(skipna=True)
print (mean_artist_hotness)
2.0

data['artist_hotness']=data.artist_hotness.mask(data.artist_hotness == 0,mean_artist_hotness)
print (data)
   artist_hotness
0             2.0
1             1.0
2             5.0
3             NaN
Run Code Online (Sandbox Code Playgroud)

或者使用loc,但省略列名:

data.loc[data.artist_hotness == 0, 'artist_hotness'] = mean_artist_hotness
print (data)
   artist_hotness
0             2.0
1             1.0
2             5.0
3             NaN

data.artist_hotness.loc[data.artist_hotness == 0, 'artist_hotness'] = mean_artist_hotness
print (data)
Run Code Online (Sandbox Code Playgroud)

索引错误:(0 True 1 False 2 False 3 False 名称:artist_hotness,dtype:bool,'artist_hotness')

另一种解决方案是DataFrame.replace指定列:

data=data.replace({'artist_hotness': {0: mean_artist_hotness}}) 
print (data)
    aa  artist_hotness
0  0.0             2.0
1  1.0             1.0
2  5.0             5.0
3  NaN             NaN 
Run Code Online (Sandbox Code Playgroud)

或者如果需要替换0所有列中的所有值:

import pandas as pd
import numpy as np

data = pd.DataFrame({'artist_hotness': [0,1,5,np.nan], 'aa': [0,1,5,np.nan]})
print (data)
    aa  artist_hotness
0  0.0             0.0
1  1.0             1.0
2  5.0             5.0
3  NaN             NaN

mean_artist_hotness = data['artist_hotness'].mean(skipna=True)
print (mean_artist_hotness)
2.0

data=data.replace(0,mean_artist_hotness) 
print (data)
    aa  artist_hotness
0  2.0             2.0
1  1.0             1.0
2  5.0             5.0
3  NaN             NaN
Run Code Online (Sandbox Code Playgroud)

如果需要NaN在所有列中替换使用DataFrame.fillna

data=data.fillna(mean_artist_hotness) 
print (data)
    aa  artist_hotness
0  0.0             0.0
1  1.0             1.0
2  5.0             5.0
3  2.0             2.0
Run Code Online (Sandbox Code Playgroud)

但如果仅在某些列中使用Series.fillna

data['artist_hotness'] = data.artist_hotness.fillna(mean_artist_hotness) 
print (data)
    aa  artist_hotness
0  0.0             0.0
1  1.0             1.0
2  5.0             5.0
3  NaN             2.0
Run Code Online (Sandbox Code Playgroud)


shi*_*vsn 9

使用pandas replace方法:

df = pd.DataFrame({'a': [1,2,3,4,0,0,0,0], 'b': [2,3,4,6,0,5,3,8]}) 

df 
   a  b
0  1  2
1  2  3
2  3  4
3  4  6
4  0  0
5  0  5
6  0  3
7  0  8

df['a']=df['a'].replace(0,df['a'].mean())

df
   a  b
0  1  2
1  2  3
2  3  4
3  4  6
4  1  0
5  1  5
6  1  3
7  1  8
Run Code Online (Sandbox Code Playgroud)