简体   繁体   English

一起附加两个数据框(Pandas,Python3)

[英]Append Two Dataframes Together (Pandas, Python3)

I am trying to append/join(?) two different dataframes together that don't share any overlapping data. 我正在尝试将不共享任何重叠数据的两个不同数据帧附加/联接(?)。

DF1 looks like DF1看起来像

  Teams        Points
  Red            2
  Green          1
  Orange         3
  Yellow         4
  ....    
  Brown          6

and DF2 looks like 和DF2看起来像

  Area         Miles
   2            3
   1            2
  ....
   7            12

I am trying to append these together using 我试图将这些附加在一起

 bigdata = df1.append(df2,ignore_index = True).reset_index()

but I get this 但我明白了

  Teams        Points
  Red            2
  Green          1
  Orange         3
  Yellow         4   
                    Area         Miles
                     2            3
                     1            2

How do I get something like this? 我如何得到这样的东西?

 Teams          Points      Area     Miles
  Red            2           2         3
  Green          1           1         2
  Orange         3
  Yellow         4

EDIT: in regards to Edchum's answers, I have tried merge and join but each create somewhat strange tables. 编辑:关于Edchum的答案,我尝试了合并和联接,但每个创建一些奇怪的表。 Instead of what I am looking for (as listed above) it will return something like this: 而不是我正在寻找的东西(如上所述),它将返回如下内容:

 Teams          Points      Area     Miles
  Red            2           2         3
  Green          1           
  Orange         3           1         2
  Yellow         4

Use concat and pass param axis=1 : 使用concat并传递参数axis=1

In [4]:

pd.concat([df1,df2], axis=1)
Out[4]:
    Teams  Points  Area  Miles
0     Red       2     2      3
1   Green       1     1      2
2  Orange       3   NaN    NaN
3  Yellow       4   NaN    NaN

join also works: join也可以:

In [8]:

df1.join(df2)
Out[8]:
    Teams  Points  Area  Miles
0     Red       2     2      3
1   Green       1     1      2
2  Orange       3   NaN    NaN
3  Yellow       4   NaN    NaN

As does merge : merge

In [11]:

df1.merge(df2,left_index=True, right_index=True, how='left')
Out[11]:
    Teams  Points  Area  Miles
0     Red       2     2      3
1   Green       1     1      2
2  Orange       3   NaN    NaN
3  Yellow       4   NaN    NaN

EDIT In the case where the indices do not align where for example your first df has index [0,1,2,3] and your second df has index [0,2] this will mean that the above operations will naturally align against the first df's index resulting in a NaN row for index row 1 . 编辑如果索引不对齐,例如您的第一个df的索引为[0,1,2,3]而第二个df的索引为[0,2]这意味着上述操作自然会与第一个df的索引导致索引行1NaN行。 To fix this you can reindex the second df either by calling reset_index() or assign directly like so: df2.index =[0,1] . 要解决此问题,您可以通过调用reset_index()或直接分配第二个df来reset_index()索引,如下所示: df2.index =[0,1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM