0

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

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.