gpt4 book ai didi

python - 使用 Pandas 保存的数据正在发生变化

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

我正在尝试使用 pandas 将一些数据保存为 csv,然后我将在其他地方加载和使用这些数据。这是我保存数据的代码

shuffledInteractionsFirstSeq = [[11, 15, 19, 9, 6, 8, 15, 9, 12, 21, 14, 7, 10, 20, 15, 6, 6, 17, 10, 10, 10, 6, 11, 10, 11, 8, 2, 16, 1, 19, 4, 9, 10, 20, 19, 17, 19, 21, 21, 6, 19, 13, 19, 20, 9, 4, 1, 17, 17, 17, 10, 5, 2, 1, 16, 3, 1, 9, 1, 21, 3, 17, 4, 19, 7, 12, 19, 20, 1, 17, 7, 1, 2, 19, 13, 17, 3, 13, 12, 13, 14, 4, 8, 19, 10, 4, 12, 19, 17, 4, 12, 5, 12, 11, 20, 9, 12, 12, 11, 19, 4, 14, 11, 7, 4, 3, 8, 8, 16, 10, 20, 3, 14, 16, 10, 9, 13, 2, 19, 9, 10, 17, 13, 10, 2, 19, 17, 10, 7, 2, 17, 12, 10, 9, 12, 1, 17, 12, 17, 9, 16, 16, 12, 20, 9, 4, 11, 3, 15, 6, 4, 8, 9, 12, 2, 16, 5, 9, 19, 17, 17, 16, 8, 15, 12, 9, 11, 14, 9, 4, 21, 1, 10, 5, 21, 9, 10, 3, 19, 19, 13, 8, 3, 12, 3, 12, 17, 16, 21, 9, 10, 8, 12, 2, 12, 17, 16, 19, 8, 17, 14, 1, 2, 13, 9, 19, 16, 5, 4, 13, 8, 13, 8, 7, 21, 2, 1, 13, 1, 6, 5, 1, 8, 10, 9, 2, 12, 3, 9, 9, 5, 12, 6, 16, 6, 13, 2, 17, 12, 19, 16, 17, 19, 14, 2, 17, 7, 6, 8, 15, 13, 19, 19, 16, 17, 14, 10, 10, 10, 12, 6, 16, 10, 1, 4, 4, 6, 19, 19, 8, 15, 16, 4, 12, 5, 17, 3, 12, 1, 9, 17, 8, 8, 19, 14, 10, 9, 4, 16, 19, 4, 8, 12, 2, 17, 15, 13, 12, 12, 12, 17, 15, 9, 16, 8, 17, 8, 6, 13, 6, 15, 1, 5, 21, 1, 17, 6, 3, 8, 8, 6, 3, 8, 15, 14, 1, 7, 2, 12, 8, 16, 6, 4, 9, 20, 12, 12, 17, 10, 9, 14, 8, 19, 17, 9, 10, 14, 1, 14, 5, 6, 12, 9, 17, 8, 19, 5, 9, 14, 16, 16, 6, 6, 3, 13, 4, 8, 19, 11, 7, 16, 5, 12, 2, 6, 6, 4, 5, 5, 21, 2, 12, 16, 17, 14, 10, 5, 12, 16, 17, 20, 12, 12, 17, 8, 6, 13, 12, 12, 17, 12, 6, 17, 8, 17, 10, 13, 2, 15, 8, 9, 14, 8, 8, 12, 15, 20, 14, 4, 19, 6, 9, 1, 11, 21, 1, 13, 13, 8, 15, 6, 14, 8, 15, 2, 16, 16, 12, 8, 17, 6, 10, 10, 10, 17, 15, 3, 6, 6, 9, 4, 8, 16, 12, 17, 17, 4, 8, 5, 15, 13, 6, 6, 6, 3, 11, 15, 3, 12, 20, 15, 16, 4, 10, 21, 9, 21, 9, 19, 19, 9, 8, 4, 13, 10, 6, 19, 1, 13, 17, 9, 1, 9, 15, 15, 19, 19, 14, 15, 4, 9, 15, 1, 19, 17, 10, 6, 1, 11, 5, 10, 6, 5, 10, 6, 1, 1, 6, 16, 17, 11, 6, 1, 15, 16, 10, 17, 10, 17, 19, 14, 1, 15, 14, 10, 10, 16, 6, 8, 19, 14, 14, 14, 12, 12, 10, 10, 15, 1, 8, 4, 1, 14, 14, 7, 10, 10, 14, 10, 17, 19, 20, 6, 8, 9, 14, 10, 14, 1, 15, 19, 10, 1, 19, 4, 15, 21, 10, 9, 3, 14, 14, 10, 10, 6, 8, 20, 6, 2, 16, 6, 9, 10, 8, 2, 17, 17, 1, 19, 13, 20, 12, 1, 16, 20, 16, 12, 9, 16, 10, 3, 14, 8, 20, 12, 12, 11, 17, 20, 11, 4, 20, 4, 15, 4, 8, 3, 12, 21, 17, 12, 10, 8, 21, 17, 10, 8, 4, 4, 16, 14, 12, 14, 14, 4, 9, 12, 4, 14, 4, 10, 10, 4, 10, 3, 9, 20, 1, 16, 10, 20, 12, 20, 5, 3, 8, 16, 9, 20, 10, 20, 21, 8, 9, 8, 5, 8, 11, 8, 19, 6, 6, 10, 19, 6, 10, 15, 8, 19, 5, 17, 19, 10, 16, 8, 19, 12, 15, 19, 15, 14, 6, 21, 16, 13, 10, 16, 5, 14, 17, 15, 5, 13, 1, 13, 15, 6, 13, 3, 15, 13, 4, 6, 8, 4, 4, 4, 6, 6, 4, 15, 3, 15, 3, 15, 16, 16, 13, 10, 19, 7, 6, 10, 10, 1, 10, 8, 20, 3, 3, 10, 15, 16, 10, 2, 10, 5, 16, 21, 7, 15, 10, 15, 3, 10, 8, 10, 8, 1, 1, 15, 8, 19, 4, 10, 10, 6, 15, 15, 6, 20, 4, 1, 10, 9, 21, 20, 6, 12, 10, 10, 14, 21, 20, 8, 14, 4, 10, 9, 12, 16, 1, 19, 16, 10, 5, 3, 1, 8, 1, 8, 1, 19, 1, 4, 6, 17, 3, 15, 8, 8, 4, 19, 1, 14, 15, 8, 6, 15, 1, 5, 10, 7, 8, 13, 15, 15, 8, 15, 14, 6, 5, 4, 15, 1, 10, 10]]
shuffledInteractionsSecondSeq = [[11, 15, 17, 10, 1, 8, 10, 1, 1, 8, 10, 10, 19, 1, 10, 14, 1, 14, 1, 4, 13, 10, 14, 1, 15, 1, 3, 4, 19, 1, 1, 1, 13, 4, 14, 8, 1, 1, 3, 8, 13, 4, 19, 19, 19, 16, 10, 1, 20, 3, 4, 16, 10, 1, 13, 9, 7, 13, 6, 16, 15, 9, 12, 11, 1, 2, 21, 2, 15, 8, 13, 1, 2, 8, 1, 6, 4, 15, 15, 21, 6, 17, 2, 8, 21, 14, 6, 15, 10, 20, 1, 5, 2, 2]]
print(max([len(seq) for seq in shuffledInteractionsFirstSeq]))
# 847
print(max([len(seq) for seq in shuffledInteractionsSecondSeq]))
# 94

DataFrame(data={'Sequence1' : shuffledInteractionsFirstSeq, 'Sequence2' : shuffledInteractionsSecondSeq}).to_csv('interactionDataSet.csv', index = False)

interactionsCSV = read_csv('interactionDataSet.csv')
interactionsSequence1 = list(interactionsCSV.get('Sequence1'))
interactionsSequence2 = list(interactionsCSV.get('Sequence2'))

print(max([len(seq) for seq in interactionsSequence1]))
# 3026
print(max([len(seq) for seq in interactionsSequence2]))
# 327

如您所见,数据在保存后发生了变化。我知道也许我做错了什么,但我想不通

最佳答案

发生这种情况是因为 read_csv 无法识别像 list 这样的复杂类型并将它们读取为字符串:

type(interactionsCSV.at[0, 'Sequence1'])
# <class 'str'>

一种可能的解决方法是使用 pandas.eval功能:

interactionsCSV['Sequence1'] = pd.eval(interactionsCSV['Sequence1'])
type(interactionsCSV.at[0, 'Sequence1'])
# <class 'list'>
max([len(s) for s in interactionsCSV.get('Sequence1')])
# 847

关于python - 使用 Pandas 保存的数据正在发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49907624/

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