I have a dataframe full of strings that I would rather have as integers, or where that is not possible, floats.
I have tried to achieve this using try: except:.
The string "30" becomes an integer, but the string "40.0" becomes a float. I would like a column containing only whole-numbers as strings ("35.0", "41.0", "55.0") to become integers.
Here is a version of what I have attempted:
import pandas as pd
data = {
'Name':['Tom', 'Dick', 'Harry'],
'Age':['20', '21', '19'],
'Height':['1.73', '1.80', '1.59'],
'Score':['72.0', '69.0', '68.0']
}
df = pd.DataFrame(data)
# Make list for looping through columns
cols = df.columns
# Check data types before manipulation
print(df.dtypes)
# Change to integer, or where not possible, float
for col in cols:
try:
df[col] = df[col].astype('int')
continue
except ValueError:
pass
try:
df[col] = df[col].astype('float')
except ValueError:
pass
# Check if manipulation was successful
print(df.dtypes)
>Solution :
As Michael already commented, you can use pd.to_numeric with errors ignore.
for col in cols:
df[col] = pd.to_numeric(df[col], errors='ignore')
No need for try excepts then.