如何替换pandas Dataframe中的非整数值?

lat*_*ish 4 python dataframe pandas

我有一个由两列组成的数据框,Age和Salary

Age   Salary
21    25000
22    30000
22    Fresher
23    2,50,000
24    25 LPA
35    400000
45    10,00,000
Run Code Online (Sandbox Code Playgroud)

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

jez*_*ael 9

如果需要替换非数值,请使用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
Run Code Online (Sandbox Code Playgroud)