0

I'd like to convert any cell that is type float to type int:

My thought was use lambda functions like this:

df["ev_id"].apply(lambda x: [int(x) if x is type(float)])

But this raises a syntax error which I'm not able to catch. Any help would be much appreciated.

Original data:

ev_id
" "
343.0
234.0
" "
212.0
"Temp"

Transformed:

ev_id
" "
343
234
" "
212
"Temp"
madsthaks
  • 2,091
  • 6
  • 25
  • 46

2 Answers2

0

You can use update by integer values - first use to_numeric with errors='coerce' for replace non numeric to NaNs, then remove them by dropna and cast:

s = pd.to_numeric(df['ev_id'], errors='coerce').dropna().astype(int)
print (s)
1    343
2    234
4    212
Name: ev_id, dtype: int32

df['ev_id'].update(s)
print (df)
  ev_id
0      
1   343
2   234
3      
4   212
5  Temp
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
0

The lambda function you want:

lambda x: int(x) if isinstance(x, float) else x
iBug
  • 35,554
  • 7
  • 89
  • 134