简体   繁体   中英

How to change object column in Data Frame to numeric column in Python?

I have Data Frame like below:

在此处输入图像描述

Both column are as type "object". I need to change column "x" bacuse I want to create a box plot using these data. How can I convert column X to numeric? I tried for example: pd.to_numeric(df1.x) nevertheless it generates error: ValueError: Unable to parse string "1,418997646" at position 0 . DO you have other idea how to change type of this column?

As suggested by @Quang:

df1['x'] = pd.to_numeric(df1['x'].str.replace(',', '.'))

You need to replace , with . in your strings before you convert them to numeric since they use comma instead of dot for decimal point.

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