Skip to content
Advertisement

Pandas save to_csv format not tabbing spaces

I want to write some data using a panda frame to a .dat file. Saving isnt an issue its formating. I’ve used sep = ‘t’ but it only some times places a tab between data.

My code looks like this:

data.reindex(columns = ['n','Burn_Gain','V','I','I_smooth']).to_csv(dat_file_path,index = False,
                                                                            header = False, sep = 't')

the resulting data:

1.0 1   0.01785000041127    5.435180664063e-06  5.435180664063e-06
1.0 1   0.01934999972582    5.679321289062e-06  5.679321289062e-06
1.0 1   0.02085000090301    5.89599609375e-06   5.89599609375e-06
1.0 1   0.02235000021756    6.11572265625e-06   6.11572265625e-06
1.0 1   0.0238499995321 6.4453125e-06   6.4453125e-06
1.0 1   0.0253500007093 6.658935546875e-06  6.658935546875e-06
1.0 1   0.02685000002384    6.771850585937e-06  6.771850585937e-06
1.0 1   0.02834999933839    7.015991210937e-06  7.015991210937e-06
1.0 1   0.02985000051558    7.232666015625e-06  7.232666015625e-06

Why? and how do I fix this?

Im also trying to set up my code so the save data file will have an empty line added in whenever the first column changes values. If anybody has a neater method than using f.readlines() and a loop, please let me know. Thnx!

Advertisement

Answer

This is not the sep='t fault, it is just how the file inside the data is displayed or “how it looks” to our eyes. The file itself is already separated by tabs. This is likely due to the number of digits behind decimal point being different.

I would recommend the following code to make all of it standardized (e.g. 3 digits behind the decimal point)

data.reindex(columns = ['n','Burn_Gain','V','I','I_smooth']).to_csv('pandasfile.csv', float_format='%.3f')
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement