57

I have two pandas dataframes.

noclickDF = DataFrame([[0, 123, 321], [0, 1543, 432]],
                      columns=['click', 'id', 'location'])
clickDF = DataFrame([[1, 123, 421], [1, 1543, 436]],
                      columns=['click', 'location','id'])

I simply want to join such that the final DF will look like:

click  |  id   |   location
0         123        321
0         1543       432
1         421        123
1         436       1543

As you can see the column names of both original DF's are the same, but not in the same order. Also there is no join in a column.

buhtz
  • 10,774
  • 18
  • 76
  • 149
redrubia
  • 2,256
  • 6
  • 33
  • 47

3 Answers3

86

You could also use pd.concat:

In [36]: pd.concat([noclickDF, clickDF], ignore_index=True)
Out[36]: 
   click    id  location
0      0   123       321
1      0  1543       432
2      1   421       123
3      1   436      1543

Under the hood, DataFrame.append calls pd.concat. DataFrame.append has code for handling various types of input, such as Series, tuples, lists and dicts. If you pass it a DataFrame, it passes straight through to pd.concat, so using pd.concat is a bit more direct.

Alex L
  • 8,748
  • 5
  • 49
  • 75
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • 2
    Great and descriptive answer. Voted up and suggested addition of `ignore_index`. – Sid Jul 17 '18 at 16:59
17

For future users (sometime >pandas 0.23.0):

You may also need to add sort=True to sort the non-concatenation axis when it is not already aligned (i.e. to retain the OP's desired concatenation behavior). I used the code contributed above and got a warning, see Python Pandas User Warning. The code below works and does not throw a warning.

In [36]: pd.concat([noclickDF, clickDF], ignore_index=True, sort=True)
Out[36]: 
   click    id  location
0      0   123       321
1      0  1543       432
2      1   421       123
3      1   436      1543
Beau Hilton
  • 413
  • 5
  • 7
9

You can use append for that

 df = noclickDF.append(clickDF)
 print df 

    click    id  location
 0      0   123       321  
 1      0  1543       432
 0      1   421       123
 1      1   436      1543

and if you need you can reset the index by

df.reset_index(drop=True)
print df
   click    id  location
0      0   123       321
1      0  1543       432
2      1   421       123
3      1   436      1543
greole
  • 4,523
  • 5
  • 29
  • 49