I am using read_sql_table() to fetch a data from sql to python where the data after import looks like:
column1 column2 column3
1.0 868.0 76225.0
0.0 2767.0 2763.0
When I read this table into a dataframe it's getting converted to float. Since I need those columns as integer, I'm using:
df['column2']=df['column2'].fillna(0).astype('int'). (Using fillna(0) as there are Nan values)
But I also want to convert all the zeroes (due to fillna(0)) back to NaN.
If I try df['column2'].replace(0, np.nan, inplace=True), this not only converts the zeroes to Nan, but also integer back to float.
Any help on how to convert float to integer using read_sql_table, without changing Nan to 0.
Thanks !!
NaNis a float , hence it converts the dtype to float , you might want to consider a Nullable Integer datatype as a dtype as integer with NaN presentTypeError: cannot safely cast non-equivalent object to int64when I trydf['column2'].fillna(0).astype('Int64')@anky