简体   繁体   中英

How do convert an entire column string to a float within a dataframe using pandas?

I have a column in my df called size

df['Size']

0         19M
1         14
2        8.7
3         25
4        2.8M
5        5.6

I wanted to remove all M in this column so i did

df.Size.str.replace('M','')

and it worked, however I also want to convert the string in this column to float.

I tried df.Size.float.replace('M','')

But i am getting this error:

AttributeError: 'Series' object has no attribute 'float'

What should i do?

I am using to_numeric

Update

pd.to_numeric(df.Size.replace('M','',regex=True),errors='coerce').fillna(df.Size)
Out[497]: 
0     19
1    14k
2    8.7
3     25
4    2.8
5    5.6
Name: Size, dtype: object

Check the conversion here only the cell contain k still str type , all other become float

pd.to_numeric(df.Size.replace('M','',regex=True),errors='coerce').fillna(df.Size).apply(type)
Out[501]: 
0    <class 'float'>
1      <class 'str'>
2    <class 'float'>
3    <class 'float'>
4    <class 'float'>
5    <class 'float'>
Name: Size, dtype: object

Data input

df
Out[500]: 
   Size
0   19M
1   14k
2   8.7
3    25
4  2.8M
5   5.6

To be safe, we can use regex to remove all letters:

df['Size'] = df['Size'].str.replace('([A-Za-z])', '', regex=True).astype(float)

print(df)
    Size
0   19.0
1   14.0
2    8.7
3   25.0
4    2.8
5    5.6
6  201.0

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