大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
对我来说工作:
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
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/170020.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...