Convert string in floats in dataframe

Question:

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

Asked By: Coco

||

Answers:

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
Answered By: T C Molenaar

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
Answered By: Shahab Rahnama
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.