pandas to_csv: suppress scientific notation in csv file when writing pandas to csv
For python 3.xx (
Python 3.7.2
)&
In [2]: pd.__version__
Out[2]: '0.23.4'
:
For visualization of the dataframe pandas.set_option
import pandas as pd #import pandas package# for visualisation fo the float data once we read the float data:pd.set_option('display.html.table_schema', True) # to can see the dataframe/table as a htmlpd.set_option('display.precision', 5) # setting up the precision point so can see the data how looks, here is 5df = pd.DataFrame(np.random.randn(20,4)* 10 ** -12) # create random dataframe
Output of the data:
df.dtypes # check datatype for columns[output]:0 float641 float642 float643 float64dtype: object
Dataframe:
df # output of the dataframe[output]:0 1 2 30 -2.01082e-12 1.25911e-12 1.05556e-12 -5.68623e-131 -6.87126e-13 1.91950e-12 5.25925e-13 3.72696e-132 -1.48068e-12 6.34885e-14 -1.72694e-12 1.72906e-123 -5.78192e-14 2.08755e-13 6.80525e-13 1.49018e-124 -9.52408e-13 1.61118e-13 2.09459e-13 2.10940e-135 -2.30242e-13 -1.41352e-13 2.32575e-12 -5.08936e-136 1.16233e-12 6.17744e-13 1.63237e-12 1.59142e-127 1.76679e-13 -1.65943e-12 2.18727e-12 -8.45242e-138 7.66469e-13 1.29017e-13 -1.61229e-13 -3.00188e-139 9.61518e-13 9.71320e-13 8.36845e-14 -6.46556e-1310 -6.28390e-13 -1.17645e-12 -3.59564e-13 8.68497e-1311 3.12497e-13 2.00065e-13 -1.10691e-12 -2.94455e-1212 -1.08365e-14 5.36770e-13 1.60003e-12 9.19737e-1313 -1.85586e-13 1.27034e-12 -1.04802e-12 -3.08296e-1214 1.67438e-12 7.40403e-14 3.28035e-13 5.64615e-1415 -5.31804e-13 -6.68421e-13 2.68096e-13 8.37085e-1316 -6.25984e-13 1.81094e-13 -2.68336e-13 1.15757e-1217 7.38247e-13 -1.76528e-12 -4.72171e-13 -3.04658e-1318 -1.06099e-12 -1.31789e-12 -2.93676e-13 -2.40465e-1319 1.38537e-12 9.18101e-13 5.96147e-13 -2.41401e-12
And now write to_csv using the float_format='%.15f' parameter
df.to_csv('estc.csv',sep=',', float_format='%.15f') # write with precision .15
file output:
,0,1,2,30,-0.000000000002011,0.000000000001259,0.000000000001056,-0.0000000000005691,-0.000000000000687,0.000000000001919,0.000000000000526,0.0000000000003732,-0.000000000001481,0.000000000000063,-0.000000000001727,0.0000000000017293,-0.000000000000058,0.000000000000209,0.000000000000681,0.0000000000014904,-0.000000000000952,0.000000000000161,0.000000000000209,0.0000000000002115,-0.000000000000230,-0.000000000000141,0.000000000002326,-0.0000000000005096,0.000000000001162,0.000000000000618,0.000000000001632,0.0000000000015917,0.000000000000177,-0.000000000001659,0.000000000002187,-0.0000000000008458,0.000000000000766,0.000000000000129,-0.000000000000161,-0.0000000000003009,0.000000000000962,0.000000000000971,0.000000000000084,-0.00000000000064710,-0.000000000000628,-0.000000000001176,-0.000000000000360,0.00000000000086811,0.000000000000312,0.000000000000200,-0.000000000001107,-0.00000000000294512,-0.000000000000011,0.000000000000537,0.000000000001600,0.00000000000092013,-0.000000000000186,0.000000000001270,-0.000000000001048,-0.00000000000308314,0.000000000001674,0.000000000000074,0.000000000000328,0.00000000000005615,-0.000000000000532,-0.000000000000668,0.000000000000268,0.00000000000083716,-0.000000000000626,0.000000000000181,-0.000000000000268,0.00000000000115817,0.000000000000738,-0.000000000001765,-0.000000000000472,-0.00000000000030518,-0.000000000001061,-0.000000000001318,-0.000000000000294,-0.00000000000024019,0.000000000001385,0.000000000000918,0.000000000000596,-0.000000000002414
And now write to_csv using the float_format='%f' parameter
df.to_csv('estc.csv',sep=',', float_format='%f') # this will remove the extra zeros after the '.'
Use the float_format
argument:
In [11]: df = pd.DataFrame(np.random.randn(3, 3) * 10 ** 12)In [12]: dfOut[12]: 0 1 20 1.757189e+12 -1.083016e+12 5.812695e+111 7.889034e+11 5.984651e+11 2.138096e+112 -8.291878e+11 1.034696e+12 8.640301e+08In [13]: print(df.to_string(float_format='{:f}'.format)) 0 1 20 1757188536437.788086 -1083016404775.687134 581269533538.1702881 788903446803.216797 598465111695.240601 213809584103.1124572 -829187757358.493286 1034695767987.889160 864030095.691202
Which works similarly for to_csv:
df.to_csv('df.csv', float_format='{:f}'.format, encoding='utf-8')
If you would like to use the values as formated string in a list, say as part of csvfile csv.writier, the numbers can be formated before creating a list:
with open('results_actout_file','w',newline='') as csvfile: resultwriter = csv.writer(csvfile, delimiter=',') resultwriter.writerow(header_row_list) resultwriter.writerow(df['label'].apply(lambda x: '%.17f' % x).values.tolist())