pandas to_csv: suppress scientific notation in csv file when writing pandas to csv

后端 未结 3 550
我在风中等你
我在风中等你 2021-02-06 01:34

I am writing a pandas df to a csv. When I write it to a csv file, some of the elements in one of the columns are being incorrectly converted to scientific notation/numbers. Fo

3条回答
  •  孤独总比滥情好
    2021-02-06 01:53

    Use the float_format argument:

    In [11]: df = pd.DataFrame(np.random.randn(3, 3) * 10 ** 12)
    
    In [12]: df
    Out[12]:
                  0             1             2
    0  1.757189e+12 -1.083016e+12  5.812695e+11
    1  7.889034e+11  5.984651e+11  2.138096e+11
    2 -8.291878e+11  1.034696e+12  8.640301e+08
    
    In [13]: print(df.to_string(float_format='{:f}'.format))
                         0                     1                   2
    0 1757188536437.788086 -1083016404775.687134 581269533538.170288
    1  788903446803.216797   598465111695.240601 213809584103.112457
    2 -829187757358.493286  1034695767987.889160    864030095.691202
    

    Which works similarly for to_csv:

    df.to_csv('df.csv', float_format='{:f}'.format, encoding='utf-8')
    

提交回复
热议问题