python - 数据框有正确的行数,但 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
解决方案
您不能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 ------ |
推荐阅读
- latex - 使用外语的乳胶导致引用错误
- javascript - Ionic 3:单击按钮时增加项目数
- jenkins - Jenkins Pipeline - 重新启动时如何获取内部版本号?
- python - 在我的网络抓取中,一些 div 没有出现
- django - Django 无法显示保存在数据库中的图片
- python - 通过浏览器访问端点时,CreateAPIView 使用 st() 函数返回 TypeError
- ios - 从 Laravel 发送 Firebase 推送通知
- graphql - 如何将 GraphQL 查询中的数据传递到以编程方式生成的页面上?
- python - 将两个数据框合并为一个新的数据框,其中唯一的项目标记为 1 或 0
- angular - 如何使用httpclient作为二进制文件以角度加载字体文件(ttf和otf)以使用opentype.js进行处理