I am trying to export my dataframe to csv with values in column POSITION formatted as integers not as float. After import from csv to df my data is correctly displayed:

POSITION
1.0
2.0
3.0
4.0

Then during some operations (sort_values, drop_duplicates, add empty values with np.nan) on POSITION column data gets changed to float:

POSITION
1.0
2.0
NULL
3.0
4.0

I have managed to convert df back to integer using astype(np.int64) method (pd.to_numeric did not worked) and unwanted zeros has disappear. But all this is gone during export to csv. I also have NULL values in this column so I have replaced them to some fake value which I wanted to reset back to NULL. But replacing fake value brought back unwanted float formatting.


This has not removed float:

df['POSITION'] = df['POSITION'].apply(pd.to_numeric)

This has removed floating but added dummy value "-1":

df['POSITION'] = df['POSITION'].fillna(-1).astype(np.int64)

This has removed dummy value "-1" but also every value in POSITION column:

df['POSITION'] = df.POSITION.replace(-1, np.nan, inplace=True)

If I've alternatively to "replace" used below code I got "-1" removed but float format was back again:

df.loc[df['POSITION'] == -1, 'POSITION'] = np.nan

0 Answers