0

I've below Python3 dataframe, when i'm writting it to csv, all integer columns (Col3,Col5,Col8) are converting into float. Is there any simple way to keep them as integers only. Like this i've more 100 columns in another dataframe.

Ex:
Col1,Col2,Col3,Col4,Col5,Col6,Col7,Col8
1960,-1,3641,ABC,98,19,LA,39
2009,-1,2992,KBT,50,91,SS,21

df.to_csv("C:\\test.csv", index=False)
print(df)

Col1,Col2,Col3,Col4,Col5,Col6,Col7,Col8
1960,-1,3641.0,ABC,98.0,19,LA,39.0
2009,-1,2992.0,KBT,50.0,91,SS,21.0

Extra .0 should be prevented in a simple way while exporting to csv.

Thanks

**Solution:**
df['Col3']=df['Col3'].fillna(0).astype('int')
df['Col5']=df['Col5'].fillna(0).astype('int')
df['Col8']=df['Col8'].fillna(0).astype('int')
RK.
  • 571
  • 4
  • 13
  • 29

0 Answers0