2016-01-20 186 views
0

我pd.series看起来是这样的:Python的熊猫系列结合了行

df.head() 
0  status parentName name  describe parent... 
1  status parentName name  describe parent... 
2  status parentName name  describe parent... 
3  status parentName name  describe parent... 
4  status parentName name  describe parent... 
Name: destinationurl, dtype: object 

和每一行是一个数据帧,它看起来像这样:

status parentName name describe parentDescribe parentEnName parentID id enName 
     0 0 IT 电子邮箱 提供电子邮箱服务的站点。  Information Technology 25 144 Email 

现在我想用应用功能合并所有行,然后将其转为数据帧。就像R中的'rbind'函数一样。

我该如何用Python Pandas做到这一点?

+0

做这项工作:'pd.concat(df.tolist())'? – EdChum

+0

是的,它的工作!你这么快,这真的帮了我很多! – jjdblast

回答

0

您可以使用pd.concat并在您Series致电tolist

In [144]: 
s = pd.Series([pd.DataFrame(data=np.random.randn(5,3), columns=list('abc')), pd.DataFrame(data=np.random.randn(5,3), columns=list('abc')), pd.DataFrame(data=np.random.randn(5,3), columns=list('abc'))]) 
s 

Out[144]: 
0    a   b   c 
0 0.295349 -1... 
1    a   b   c 
0 -0.380644 0... 
2    a   b   c 
0 0.329135 1... 
dtype: object 

In [149]: 
pd.concat(s.tolist(), ignore_index=True) 

Out[149]: 
      a   b   c 
0 0.295349 -1.128448 -0.674335 
1 0.413450 0.211495 0.695035 
2 -1.983857 -0.795089 -1.807442 
3 -0.366494 -1.784717 1.257727 
4 -0.651171 1.430601 -0.729049 
5 -0.380644 0.986193 0.146934 
6 -0.551766 -0.048919 0.315231 
7 -0.649579 0.252312 -2.307680 
8 -0.715894 -0.134816 0.103490 
9 -0.582027 -0.487878 0.836762 
10 0.329135 1.266439 -0.071934 
11 -0.022002 0.664152 -0.159218 
12 -1.411058 0.046058 1.467763 
13 0.116095 -2.731663 -0.448027 
14 -0.320958 0.587676 -0.654869 
+0

是的,它的工作!我刚刚进入了一个错误的方式来执行此操作,并且您的代码非常干净清晰,非常感谢您! – jjdblast

+0

不用担心,请记住接受我的回答,所以这个问题并没有得到解答,我的答案左上角会出现一个空的刻度标记,请注意,在一个Series中存储dfs是很奇怪和有问题的。你最好只是在列表或字典中存储,因为你并没有真正使用该系列,而只是一个有效阵列 – EdChum

+0

好的,我会关闭这个问题并尝试修复我的代码〜这是一个很好的体验向你学习! – jjdblast