ssmm
ssmm

Reputation: 159

Convert Multiple Columns from String to Float and Int

I'm trying to convert multiple columns in my df from string to int and float. I used fillna method to get rid of NaN values however I'm still getting an error. Here's what I did:

df = [['column1', 'column2', 'column3', 'column4']]

df = df[['column1', 'column2', 'column3', 'column4']].fillna(0)
convert_dict = {'column1': int,
                 'column2': float,
                  'column3': int, 
                    'column4': float}
df = df.astype(convert_dict)

The error says ValueError: cannot convert float NaN to integer

Edit: Removed inplace=True

Upvotes: 1

Views: 1087

Answers (1)

jsmart
jsmart

Reputation: 3001

You could use Int64, which supports missing integer values:

import numpy as np
import pandas as pd

df = pd.DataFrame({'A': [1, 2, None, 4], 
                   'B': [1.0, 2.0, 3.0, None]})

convert_dict = {'A': 'Int64', 'B': float}
convert_dict

for field, new_type in convert_dict.items():
    df[field] = df[field].astype(new_type)

print(df)
print(df.dtypes)

      A    B
0     1  1.0
1     2  2.0
2  <NA>  3.0
3     4  NaN

A      Int64
B    float64
dtype: object

Upvotes: 1

Related Questions