如何将熊猫中的数据框组合在一起?

时间:2015-03-10 12:09:54

标签: python python-2.7 pandas

list_1  =[1, 2, 3, 4 ]

def fun(list_1):
    for each value in list1:
        # perform some operation and create a new data frame(pandas) for each value in the list
        # so in total I should get 4 data frames.

        yield new_data_frame

当我打电话给乐趣(list_1)时,我得到4个数据帧:

data_frame_1

data_frame_2

data_frame_3

data_frame_4

我的问题是,如果我需要垂直组合pandas数据帧怎么办?

data_frame_1
+
data_frame_2
+
data_frame_3
+
data_frame_4

1 个答案:

答案 0 :(得分:2)

将它们存储在列表中,然后pd.concat(df_list, ignore_index=True)

pd.concat([data_frame_1, data_frame_2, data_frame_3, data_frame_4],ignore_index=True)

更简单,只需将arg的乐趣传递给pd.concat

In [8]:

list_1 = [1,2,3,4]
def fun(list_1):
    for each_value in list_1:
        # perform some operation and create a new data frame(pandas) named "new_data_frame" for each value in the list

        yield pd.DataFrame({'a':[each_value]})
pd.concat(fun(list_1), ignore_index=True)
Out[8]:
   a
0  1
1  2
2  3
3  4