大家好,又见面了,我是你们的朋友全栈君。
对我来说工作:
df.ix[df[‘Type’] == ‘Dog’, ‘Killed’] = df.ix[df[‘Type’] == ‘Dog’, ‘Killed’].fillna(2.25)
print (df)
Type Killed Survived
0 Dog 5.00 2
1 Dog 3.00 4
2 Cat 1.00 7
3 Dog 2.25 3
4 cow NaN 2
如果系列需要fillna – 因为2列被杀和幸存:
m = df[df[‘Type’] == ‘Dog’].mean().round()
print (m)
Killed 4.0
Survived 3.0
dtype: float64
df.ix[df[‘Type’] == ‘Dog’] = df.ix[df[‘Type’] == ‘Dog’].fillna(m)
print (df)
Type Killed Survived
0 Dog 5.0 2
1 Dog 3.0 4
2 Cat 1.0 7
3 Dog 4.0 3
4 cow NaN 2
如果需要fillna只在Killed列中:
#if dont need rounding, omit it
m = round(df.ix[df[‘Type’] == ‘Dog’, ‘Killed’].mean())
print (m)
4
df.ix[df[‘Type’] == ‘Dog’, ‘Killed’] = df.ix[df[‘Type’] == ‘Dog’, ‘Killed’].fillna(m)
print (df)
Type Killed Survived
0 Dog 5.0 2
1 Dog 3.0 8
2 Cat 1.0 7
3 Dog 4.0 3
4 cow NaN 2
您可以重用以下代码:
filtered = df.ix[df[‘Type’] == ‘Dog’, ‘Killed’]
print (filtered)
0 5.0
1 3.0
3 NaN
Name: Killed, dtype: float64
df.ix[df[‘Type’] == ‘Dog’, ‘Killed’] = filtered.fillna(filtered.mean())
print (df)
Type Killed Survived
0 Dog 5.0 2
1 Dog 3.0 8
2 Cat 1.0 7
3 Dog 4.0 3
4 cow NaN 2
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/170020.html原文链接:https://javaforall.cn