简体   繁体   中英

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

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

and DF2 looks like

  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. 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 :

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:

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 :

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 . To fix this you can reindex the second df either by calling reset_index() or assign directly like so: df2.index =[0,1] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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