【发布时间】:2015-08-08 20:50:23
【问题描述】:
我有一个 pandas Series 对象,每个值都是一个 DataFrame。我正在尝试将其转换为单个DataFrame,其中所有Series 值(单个DataFrame)堆叠在一起。如何在没有循环的情况下实现这一点?
下面的玩具示例用于生成测试对象 (results)。
import pandas as pd
import numpy as np
numrows = 10000
def toy_function(x):
silly_sequence = np.random.uniform(10, 100, (x+1))
toy = pd.DataFrame({'ID':pd.Series(np.random.random_integers(1,20,3)),'VALUE':pd.Series((np.median(silly_sequence),np.mean(silly_sequence), np.max(silly_sequence)))})
return toy
results = pd.DataFrame({'ID':range(numrows)})['ID'].apply(toy_function)
results 属于Series 类型,每个元素都是DataFrame,如下所示:
In [1]: results[1]
Out[1]:
ID VALUE
0 17 40.035398
1 8 40.035398
2 20 66.483083
我正在寻找一种将results[1]、results[2] 等相互堆叠以产生如下 DataFrame 的方法:
ID VALUE
0 17 40.035398
1 8 40.035398
2 20 66.483083
4 12 25.035398
5 1 25.135398
6 19 65.553083
...
【问题讨论】: