Output different precision by column with pandas.DataFrame.to_csv()? Output different precision by column with pandas.DataFrame.to_csv()? numpy numpy

Output different precision by column with pandas.DataFrame.to_csv()?


Change the type of column "vals" prior to exporting the data frame to a CSV file

df_data['vals'] = df_data['vals'].map(lambda x: '%2.1f' % x)df_data.to_csv(outfile, index=False, header=False, float_format='%11.6f')


The more current version of hknust's first line would be:

df_data['vals'] = df_data['vals'].map(lambda x: '{0:.1}'.format(x))

To print without scientific notation:

df_data['vals'] = df_data['vals'].map(lambda x: '{0:.1f}'.format(x)) 


You can use round method for dataframe before saving the dataframe to the file.

df_data = df_data.round(6)df_data.to_csv('myfile.dat')