gpt4 book ai didi

python - 如何替换 Pandas 数据框中的非整数值?

转载 作者:太空狗 更新时间:2023-10-30 02:02:15 26 4
gpt4 key购买 nike

我有一个由两列组成的数据框,年龄和薪水

Age   Salary
21 25000
22 30000
22 Fresher
23 2,50,000
24 25 LPA
35 400000
45 10,00,000

如何处理 Salary 列中的异常值并用整数替换它们?

最佳答案

如果需要替换非数值,请使用 to_numeric带有参数 errors='coerce':

df['new'] = pd.to_numeric(df.Salary.astype(str).str.replace(',',''), errors='coerce')
.fillna(0)
.astype(int)
print (df)
Age Salary new
0 21 25000 25000
1 22 30000 30000
2 22 Fresher 0
3 23 2,50,000 250000
4 24 25 LPA 0
5 35 400000 400000
6 45 10,00,000 1000000

关于python - 如何替换 Pandas 数据框中的非整数值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42929997/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com