Appending two dataframes with same columns, different order
You could also use pd.concat:
In [36]: pd.concat([noclickDF, clickDF], ignore_index=True)Out[36]: click id location0 0 123 3211 0 1543 4322 1 421 1233 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.
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 location0 0 123 3211 0 1543 4322 1 421 1233 1 436 1543
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 location0 0 123 3211 0 1543 4322 1 421 1233 1 436 1543