Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas - convert float to int when there are NaN values in the column

Tags:

pandas

I'm trying to convert float numbers to int in my df column with this one liner:

df['id'] = df['id'].map(lambda x: int(x))

But some values are NaN an will throw:

ValueError: cannot convert float NaN to integer

How do I fix this?

like image 902
8-Bit Borges Avatar asked Oct 24 '20 02:10

8-Bit Borges


People also ask

How do I change NaN values in a column in pandas?

You can use the fillna() function to replace NaN values in a pandas DataFrame.

How do you convert all float columns to int in pandas?

To convert a column that includes a mixture of float and NaN values to int, first replace NaN values with zero on pandas DataFrame and then use astype() to convert. Use DataFrame. fillna() to replace the NaN values with integer value zero. Yields below output.

How do I change NaN values in pandas based on condition?

You can replace all values or selected values in a column of pandas DataFrame based on condition by using DataFrame. loc[] , np. where() and DataFrame. mask() methods.


Video Answer


1 Answers

NaN is itself float and can't be convert to usual int. You can use pd.Int64Dtype() for nullable integers:

# sample data:
df = pd.DataFrame({'id':[1, np.nan]})

df['id'] = df['id'].astype(pd.Int64Dtype())

Output:

     id
0     1
1  <NA>

Another option, is use apply, but then the dtype of the column will be object rather than numeric/int:

df['id'] = df['id'].apply(lambda x: x if np.isnan(x) else int(x))
like image 77
Quang Hoang Avatar answered Oct 21 '22 13:10

Quang Hoang