如何用熊猫的平均值替换某些值



数据集

我想用年龄的平均值代替-1,但我一直得到零,而不是平均值。

这是代码代码

试试这个;

import pandas as pd
df = pd.DataFrame({"Age":[1.2,2,3,-1,0]})
# Output before;
Age
0  1.2
1  2.0
2  3.0
3 -1.0
4  0.0
df["Age"].replace(-1,df["Age"].mean(),inplace=True)
# Output after replacing;
Age
0  1.20
1  2.00
2  3.00
3  1.04
4  0.00

最新更新