根据每个单独数据帧的行索引(数量),合并/合并/合并多个数据帧

时间:2018-10-02 13:55:01

标签: python pandas join merge append

我想读取数据帧列表的第n行,并通过附加所有第N行来创建新的数据帧。

假设我们有以下数据框:

df = pd.DataFrame()

df_list = [df1, df2, df3]

for i in range(len(df1)):
    for x in df_list:
        df = df.append(x.loc[i], ignore_index = True)

我使用以下方法来获得所需的df:

>>> df
   A    B    C    D
0 -0.8 -2.8 -0.3 -0.1
1  1.4 -0.7  1.5 -1.3
2  0.3 -0.5 -1.6 -0.8
3 -0.1 -0.9  0.2 -0.7
4  1.6  1.4  1.4  0.2
5  0.2 -0.5 -1.1  1.6
6  0.7 -3.3 -1.1 -0.4
7 -1.4  0.2 -1.7  0.7
8 -0.3  0.7 -1.0  1.0 

结果如下:

df.row1 = df1.row1
df.row2 = df2.row1
df.row3 = df3.row1
df.row4 = df1.row2
df.row5 = df2.row2
df.row6 = df3.row2
...

我只是想知道是否有一种熊猫方法来重写此代码,而这会做同样的事情(也许使用.iterrows,pd.concat,pd.join或pd.merge)?

欢呼

更新 只是在一个df后面追加一个df,并不是我在这里想要的。

代码应执行以下操作:

{{1}}

4 个答案:

答案 0 :(得分:2)

pd.concat

df=pd.concat([df1,df2,df3]).reset_index(drop=True)

Jez推荐

df=pd.concat([df1,df2,df3],ignore_index=True)

答案 1 :(得分:1)

尝试:

>>> df1 = pd.DataFrame({'A':['-0.8', '-0.1', '0.7'],
...                     'B':['-2.8', '-0.9', '-3.3'],
...                      'C':['-0.3', '0.2', '-1.1'],
...                      'D':['-0.1', '-0.7', '-0.4']})
>>>
>>> df2 = pd.DataFrame({'A':['1.4', '1.6', '-1.4'],
...                     'B':['-0.7', '1.4', '0.2'],
...                      'C':['1.5', '1.4', '-1.7'],
...                      'D':['-1.3', '0.2', '0.7']})
>>>

>>> df3 = pd.DataFrame({'A':['0.3', '0.2', '-0.3'],
...                     'B':['-0.5', '-0.5', '0.7'],
...                      'C':['-1.6', '-1.1', '-1.0'],
...                      'D':['-0.8', '1.6', '1.0']})

>>> df=pd.concat([df1,df2,df3],ignore_index=True)
>>> print(df)
      A     B     C     D
0  -0.8  -2.8  -0.3  -0.1
1  -0.1  -0.9   0.2  -0.7
2   0.7  -3.3  -1.1  -0.4
3   1.4  -0.7   1.5  -1.3
4   1.6   1.4   1.4   0.2
5  -1.4   0.2  -1.7   0.7
6   0.3  -0.5  -1.6  -0.8
7   0.2  -0.5  -1.1   1.6
8  -0.3   0.7  -1.0   1.0

OR

df=pd.concat([df1,df2,df3], axis=0, join='outer', ignore_index=True)

注意:

axis: whether we will concatenate along rows (0) or columns (1)
join: can be set to inner, outer, left, or right. by using outer its sort it's lexicographically
ignore_index: whether or not the original row labels from should be retained, by default False ,If True, do not use the index labels.

答案 2 :(得分:1)

对于单个输出数据帧,可以串联并按索引排序:

res = pd.concat([df1, df2, df3]).sort_index().reset_index(drop=True)

     A    B    C    D
0 -0.8 -2.8 -0.3 -0.1
1  1.4 -0.7  1.5 -1.3
2  0.3 -0.5 -1.6 -0.8
3 -0.1 -0.9  0.2 -0.7
4  1.6  1.4  1.4  0.2
5  0.2 -0.5 -1.1  1.6
6  0.7 -3.3 -1.1 -0.4
7 -1.4  0.2 -1.7  0.7
8 -0.3  0.7 -1.0  1.0

对于数据框字典,您可以串联然后按索引分组:

res = dict(tuple(pd.concat([df1, df2, df3]).groupby(level=0)))

使用如上定义的字典,每个值代表一个行号。例如,res[0]将给出每个输入数据帧的第一行。

答案 3 :(得分:0)

您可以通过以下方式将它们连接起来并保留其原始索引作为列:

df_total = pd.concat([df1.reset_index(), df2.reset_index(),
                      df3.reset_index()]) 

>> df_total
   index    A    B    C    D
0      0 -0.8 -2.8 -0.3 -0.1
1      1 -0.1 -0.9  0.2 -0.7
2      2  0.7 -3.3 -1.1 -0.4
0      0  1.4 -0.7  1.5 -1.3
1      1  1.6  1.4  1.4  0.2
2      2 -1.4  0.2 -1.7  0.7
0      0  0.3 -0.5 -1.6 -0.8
1      1  0.2 -0.5 -1.1  1.6
2      2 -0.3  0.7 -1.0  1.0

然后,您可以制作一个多索引数据框并按索引排序:

df_joined = df_total.reset_index(drop=True).reset_index()

>> df_joined
   level_0  index    A    B    C    D
0        0      0 -0.8 -2.8 -0.3 -0.1
1        1      1 -0.1 -0.9  0.2 -0.7
2        2      2  0.7 -3.3 -1.1 -0.4
3        3      0  1.4 -0.7  1.5 -1.3
4        4      1  1.6  1.4  1.4  0.2
5        5      2 -1.4  0.2 -1.7  0.7
6        6      0  0.3 -0.5 -1.6 -0.8
7        7      1  0.2 -0.5 -1.1  1.6
8        8      2 -0.3  0.7 -1.0  1.0

>> df_joined = df_joined.set_index(['index', 'level_0']).sort_index()

>> df_joined

                 A    B    C    D
index level_0                    
0     0       -0.8 -2.8 -0.3 -0.1
      3        1.4 -0.7  1.5 -1.3
      6        0.3 -0.5 -1.6 -0.8
1     1       -0.1 -0.9  0.2 -0.7
      4        1.6  1.4  1.4  0.2
      7        0.2 -0.5 -1.1  1.6
2     2        0.7 -3.3 -1.1 -0.4
      5       -1.4  0.2 -1.7  0.7
      8       -0.3  0.7 -1.0  1.0

您只需执行以下操作即可将所有这些数据放入数据框:

>>  pd.DataFrame(df_joined.values, columns = df_joined.columns)

     A    B    C    D
0 -0.8 -2.8 -0.3 -0.1
1  1.4 -0.7  1.5 -1.3
2  0.3 -0.5 -1.6 -0.8
3 -0.1 -0.9  0.2 -0.7
4  1.6  1.4  1.4  0.2
5  0.2 -0.5 -1.1  1.6
6  0.7 -3.3 -1.1 -0.4
7 -1.4  0.2 -1.7  0.7
8 -0.3  0.7 -1.0  1.0
相关问题