简体   繁体   中英

Pandas Column Values Replace

I have dataset. A name of column is wage. Column's element contain K values. I want to replace K values.

data['Wage']=data['Wage'].replace("K","")

But it doesn't work.

my code is here

You can use:

data['Wage']=data['Wage'].replace("K","",regex=True)

Or:

data['Wage']=data['Wage'].astype(str).str.replace("K","")

You can also use:

df = pd.DataFrame({'Wage': ['$200k', '$500']})
df['Wage'] = df['Wage'].str.split('k', expand=True)[0]
print(df)

Output:

   Wage
0  $200
1  $500

OR

df['Wage'] = df['Wage'].str.replace(r'k', '')
print(df)

Output:

   Wage
0  $200
1  $500

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM