Pandas Output Dataframe to_csv

I cannot find a way to control the output of each frame of column data. From the following code:

df.to_csv('dfnc.txt', 
        sep=' ', 
        float_format='%.8f',
        cols=['cycle','passs','ip','lon','lat'],
        index=False)

I get this:

1.00000000 1.00000000 543.00000000 23.15881870 -64.70485950
1.00000000 1.00000000 544.00000000 23.10356160 -64.64569150
1.00000000 1.00000000 545.00000000 23.04852510 -64.58650550
1.00000000 1.00000000 546.00000000 22.99370760 -64.52730150
1.00000000 1.00000000 547.00000000 22.93910770 -64.46807990

And I want this:

1 1 543 23.15881870 -64.70485950
1 1 544 23.10356160 -64.64569150
1 1 545 23.04852510 -64.58650550
1 1 546 22.99370760 -64.52730150
1 1 547 22.93910770 -64.46807990

Many thanks for the help.

+4
source share
1 answer

I think you can just convert 1st to 3rd columnsin int64before writing a CSV file (if you check .types, I'm sure that they are all float64):

df[['cycle', 'passs', 'ip']]=df[['cycle', 'passs', 'ip']].astype(int64)
+3
source

Source: https://habr.com/ru/post/1527408/


All Articles