gpt4 book ai didi

python - 将列添加到 DataFrame 时忽略列长度

转载 作者:太空宇宙 更新时间:2023-11-03 18:06:06 25 4
gpt4 key购买 nike

我正在尝试向 DataFrame 添加一堆返回值,以便可以将其输出到 CSV 文件并进行一些基本的绘图,但我收到一条错误消息:ValueError: Length of Values does与索引长度不匹配。我并不特别关心列有多长,但我很好奇是否可以缓解索引,以便列表中的第一个元素出现在索引 0 处,然后其他所有内容都显示为带有 的后续值如果需要的话,在末尾进行 NaN 填充。这在 Pandas 中可行吗?

for idx, col in enumerate(df.T.index.values):
print "On " + str(idx) + " of " + str(len(df.T.index.values))
startDate = dt.datetime.strptime(col.split()[0], "%m/%d/%y")
endDate = startDate + dt.timedelta(days=91)

returns1 = getReturnsForPeriod(df[col][1], startDate, endDate)
retval[(col.split()[0], df[col][1], df[col][0])] = [ret for ret in returns1]

returns2 = getReturnsForPeriod(df[col][2], startDate, endDate)
retval[(col.split()[0], df[col][2], df[col][0])] = [ret for ret in returns2]

returns3 = getReturnsForPeriod(df[col][3], startDate, endDate)
retval[(col.split()[0], df[col][3], df[col][0])] = [ret for ret in returns3]

returns4 = getReturnsForPeriod(df[col][4], startDate, endDate)
retval[(col.split()[0], df[col][4], df[col][0])] = [ret for ret in returns4]

最佳答案

我认为this是一个与你类似的问题。通过 via dictionary 来做到这一点首先,你会知道数组的最终最大长度。

这里只是这个想法的基本模型,尚未测试:

d = {}
for idx, col in enumerate(df.T.index.values):
d.update(idx,col)
df = pandas.DataFrame.from_dict({idx,Series(col)) for idx,col in d.iteritems()})

关于python - 将列添加到 DataFrame 时忽略列长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26871872/

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