首页 > 解决方案 > 数据框有正确的行数,但 CSV 没有(df.to_csv)

问题描述

我已经从 for 循环正确地创建了一个数据框,并且我知道如何将 df 保存为 CSV。数据框为 7 行(包括标题行)x 4 col。但是,CSV 只有一行(x 28 列)。

我不确定如何使我的 CSV 具有与 df 相同的形状/外观。

这是我的代码:

filename = (r'~/Downloads/try_data1.csv')

raw_data = []

for loop: 
    box = np.where(stuff)[0] 
    binaries = np.where(stuff)[0]
    candidates = np.where(stuff)[0]
    singles = np.where(stuff)[0]

    raw_data.append({'range': ['%s to %s' % (i, i+delta)], 'binaries': [len(binaries)], 'candidates': [len(candidates)],'singles': [len(singles)]})

df = pd.DataFrame(raw_data, columns = ["range", "binaries", "candidates", "singles"])

#I believe everything is good until here when
#I change the df to csv

df.to_csv(filename, line_terminator=',', index=False, header=True)

我的df:

df
Out: 
            range binaries candidates singles
0  [-1.0 to -0.5]      [1]        [0]     [2]
1   [-0.5 to 0.0]      [4]        [0]    [34]
2    [0.0 to 0.5]      [1]        [0]    [11]
3    [0.5 to 1.0]      [2]        [1]    [10]
4    [1.0 to 1.5]      [4]        [2]     [5]
5    [1.5 to 2.0]      [1]        [3]    [12]

当我在 Excel 中打开我的 CSV 文件时,它看起来像:

range   binaries    candidates  singles ['-1.0 to -0.5'] [1] [0] [2]    ['-0.5 to 0.0'] [4] [0] [34].. etc

标签: pythonpandascsvdataframe

解决方案


您不能line_terminator与 相同separator,因为无法区分新行和新字段:

### Sample Data
df = pd.DataFrame({'A': ['11111', '22222', '33333'],
                   'B': ['aaaaa', 'bbbbb', 'ccccc']})

df.to_csv()
',A,B\r\n0,11111,aaaaa\r\n1,22222,bbbbb\r\n2,33333,ccccc\r\n'
#        \-----------/                       \---------/                   
#        Lines separated                  Fields separated
#       by \r\n terminator                  by , separator

当它们变得相同时,就会有一个明显的问题

df.to_csv(line_terminator=',')
',A,B,0,11111,aaaaa,1,22222,bbbbb,2,33333,ccccc,'
#    |                                   |
#    |------ These look identical ------ |

推荐阅读