将循环结果合并到DataFrame中
问题描述:
使用Python Pandas 0.19.1。将循环结果合并到DataFrame中
我在循环中调用一个函数,它返回一个长度为4的数字列表。将它们连接成DataFrame的最简单方法是什么?
我这样做:
result = pd.DataFrame()
for t in dates:
result_t = do_some_stuff(t)
result.append(result_t, ignore_index=True)
的问题是,它沿着列而不是按行串接。如果dates
的长度为250,则它会给出1000列的单列df。相反,我想要的是一个250 x 4 df。
答
我想你需要添加所有DataFrames
列出result
然后用concat
:
result = []
for t in dates:
result.append(do_some_stuff(t))
print (pd.concat(result, axis=1))