Home > Back-end >  Convert string in floats in dataframe
Convert string in floats in dataframe

Time:09-27

I have a dataframe with string columns some of them are numbers in string and some others are just string. I want to convert the columns that are numbers in floats but not string. Someone sent me a link but it works only if you know the column and my dataframe is flexible.

>>> df_1
     A    B      C
2    M01  test   '100.0'
3    M02  test2  '80.0' 

Convert a string that contains a price with space after thousands to float in pandas column

CodePudding user response:

Hope this helps:

>>> data = {'A': {2: 'M01', 3: 'M02'},
            'B': {2: 'test', 3: 'test2'},
            'C': {2: '100.0', 3: '80.0'}}

>>> df = pd.DataFrame(data)
>>> df.dtypes
A    object
B    object
C    object
dtype: object
>>> df = df.apply(pd.to_numeric, errors='ignore')
>>> df.dtypes
A     object
B     object
C    float64
dtype: object

CodePudding user response:

You can use this:

import pandas as pd

data = {'A':['M01', 'M02'],'B':['test','test2'], 'C': ['100.0', '80.0']}

df = pd.DataFrame(data)

#find columns which are numbers  
m = df.apply(lambda s: pd.to_numeric(s, errors='coerce').notnull().all())

#convert True columns in `m` to float
for col in df.columns:
    if m[col]:
        df[col] = df[col].astype({col: 'float'})

df.dtypes

Output:

A     object
B     object
C    float64
dtype: object
  • Related