gpt4 book ai didi

python - 使用 Pandas 将多行数据导出到 csv

转载 作者:太空宇宙 更新时间:2023-11-04 05:33:58 25 4
gpt4 key购买 nike

我有一个匹配算法,可以将学生与项目联系起来。它正在运行,但我无法将数据导出到 csv 文件。当有 200 个值要导出时,它仅采用最后一个值并仅导出该值。

当我想要获取整个“s”而不是组成“s”的三个 3 数字时,导出的数据使用每个数字作为值,它们被分成三列。我附上了下面的图片。任何帮助将不胜感激。

What it looks like

What it should look like

#Imports for Pandas

import pandas as pd
from pandas import DataFrame

SPA()
for m in M:
s = m['student']
l = m['lecturer']
Lecturer[l]['limit'] = Lecturer[l]['limit'] - 1
id = m['projectid']
p = Project[id]['title']
c = Project[id]['sourceid']
r = str(getRank("Single_Projects1copy.csv",s,c))


print(s+","+l+","+p+","+c+","+r)

dataPack = (s+","+l+","+p+","+c+","+r)

df = pd.DataFrame.from_records([dataPack])
df.to_csv('try.csv')

最佳答案

你在循环中不断覆盖,所以你只得到最后一位数据,你需要用 df.to_csv('try.csv',mode="a",header= False) 或创建一个 df 并附加到它并在循环外写入,例如:

df = pd.DataFrame()
for m in M:
s = m['student']
l = m['lecturer']
Lecturer[l]['limit'] = Lecturer[l]['limit'] - 1
id = m['projectid']
p = Project[id]['title']
c = Project[id]['sourceid']
r = str(getRank("Single_Projects1copy.csv",s,c))


print(s+","+l+","+p+","+c+","+r)

dataPack = (s+","+l+","+p+","+c+","+r)

df.append(pd.DataFrame.from_records([dataPack]))
df.to_csv('try.csv') # write all data once outside the loop

更好的选择是打开一个文件并将该文件对象传递给 to_csv:

with open('try.csv', 'w') as f:
for m in M:
s = m['student']
l = m['lecturer']
Lecturer[l]['limit'] = Lecturer[l]['limit'] - 1
id = m['projectid']
p = Project[id]['title']
c = Project[id]['sourceid']
r = str(getRank("Single_Projects1copy.csv",s,c))
print(s+","+l+","+p+","+c+","+r)

dataPack = (s+","+l+","+p+","+c+","+r)
pd.DataFrame.from_records([dataPack]).to_csv(f, header=False)

你得到单独的字符,因为你使用 from_records 传递一个字符串 dataPack 作为值,所以它遍历字符:

In [18]: df = pd.DataFrame.from_records(["foobar,"+"bar"])

In [19]: df
Out[19]:
0 1 2 3 4 5 6 7 8 9
0 f o o b a r , b a r

In [20]: df = pd.DataFrame(["foobar,"+"bar"])

In [21]: df
Out[21]:
0
0 foobar,bar

我认为您基本上希望作为元组 dataPack = (s, l, p,c, r) 并使用 pd.DataFrame(dataPack)。你根本不需要 pandas,csv 库会为你做这一切,而不需要创建数据帧。

关于python - 使用 Pandas 将多行数据导出到 csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36238101/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com