My question is very similar to this one, but I need to convert my entire dataframe instead of just a series. The to_numeric function only works on one series at
apply() the pd.to_numeric with errors='ignore' and assign it back to the DataFrame:
df = pd.DataFrame({'ints': ['3', '5'], 'Words': ['Kobe', 'Bryant']})
print ("Orig: \n",df.dtypes)
df.apply(pd.to_numeric, errors='ignore')
print ("\nto_numeric: \n",df.dtypes)
df = df.apply(pd.to_numeric, errors='ignore')
print ("\nto_numeric with assign: \n",df.dtypes)
Output:
Orig:
ints object
Words object
dtype: object
to_numeric:
ints object
Words object
dtype: object
to_numeric with assign:
ints int64
Words object
dtype: object