Pandas dataframe.to_csv文件行彼此不对齐

时间:2016-06-29 14:00:29

标签: python csv pandas dataframe alignment

我想将我的DataFrame写入CSV文件。结果如下所示。行彼此不对齐。你有什么建议可以解决这个问题吗?

代码:

max_by

file.csv的内容:

++++++++++++++++++++++++
C1   c2    c3       c2
1           1.111           1.111           1.111
2         23.261           1.111           1.111
3       138.383           1.111           1.111
2       228.717           1.111           1.111
5       358.225           1.111           1.111
6       268.173           1.111           1.111
7       577.311           1.111           1.111
8       687.623           1.111           1.111
3       737.357           1.111           1.111
11       317.185           1.111           1.111
11     1116.813           1.111           1.111
12     1126.521           1.111           1.111
13     1151.325           1.111           1.111

22     2273.853           1.111           1.111
23     2583.581           1.111           1.111
22     2618.822           1.111           1.111
25           1.111           1.111         23.261
26         23.261           1.111         23.261
27       138.383           1.111         23.261
28       228.717           1.111         23.261
23       358.225           1.111         23.261

3 个答案:

答案 0 :(得分:3)

csv文件不应该以这种方式垂直对齐。它们应该用逗号分隔值。你似乎想要格式很好的ascii。

print dataframe.__repr__()

    C1        c2     c3    c2.1
0    1     1.111  1.111   1.111
1    2    23.261  1.111   1.111
2    3   138.383  1.111   1.111
3    2   228.717  1.111   1.111
4    5   358.225  1.111   1.111
5    6   268.173  1.111   1.111
6    7   577.311  1.111   1.111
7    8   687.623  1.111   1.111
8    3   737.357  1.111   1.111
9   11   317.185  1.111   1.111
10  11  1116.813  1.111   1.111
11  12  1126.521  1.111   1.111
12  13  1151.325  1.111   1.111
13  22  2273.853  1.111   1.111
14  23  2583.581  1.111   1.111
15  22  2618.822  1.111   1.111
16  25     1.111  1.111  23.261
17  26    23.261  1.111  23.261
18  27   138.383  1.111  23.261
19  28   228.717  1.111  23.261
20  23   358.225  1.111  23.261

或:

with open('./filename.txt', 'w') as fo:
    fo.write(dataframe.__repr__())

答案 1 :(得分:0)

使用分隔符标签:

dataframe.to_csv(data,sep="\t",index=False,header=False,encoding='ascii',
             float_format='%10.4f',quoting=csv.QUOTE_NONE, escapechar=" ", mode='a+')

答案 2 :(得分:0)

This can be done by fixing the number of digits and the width of column headers.

Here is how one can fix number of characters in column headers. (Here it is fixed to 10)

for name in df.columns:
    df.rename(columns={name: "{:10}".format(name)}, inplace=True)

Specify the format of numbers while writing csv file.

df.to_csv(<path>, float_format='%0.4e')

The only problem with this approach is, it is not possible to specify different formats for different columns.