0

I'm trying to convert an entire dataframe into integer, i.e. all Variables to type(int), the data has NaNs present, so was going for pd.to_numeric and coercing the errors to NaN, where I can handle them later.

But pd.to_numeric takes a list, tuple, 1-d array, or Series.

I can go on step by step by giving each column name and changing the type. But is there any way to do that optimally in few lines?

I tried this, but getting error.

change = lambda x: pd.to_numeric(df_copy[x], errors='coerce')
df_copy = pd.DataFrame(map(change, x) for x in df_copy.columns)
Saurav Joshi
  • 414
  • 5
  • 13
  • This might help... https://stackoverflow.com/questions/34844711/convert-entire-pandas-dataframe-to-integers-in-pandas-0-17-0 – run-out Feb 21 '19 at 10:53

1 Answers1

0

You can simply do this df_copy.apply(lambda x : pd.to_numeric(x, errors='coerce'))

[convert entire pandas dataframe to integers in pandas (0.17.0)