Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending two dataframes with same columns, different order

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.

like image 618
redrubia Avatar asked Jan 29 '14 15:01

redrubia


People also ask

Which are the 3 main ways of combining DataFrames together?

Combine data from multiple files into a single DataFrame using merge and concat. Combine two DataFrames using a unique ID found in both DataFrames. Employ to_csv to export a DataFrame in CSV format. Join DataFrames using common fields (join keys).


1 Answers

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.

like image 103
unutbu Avatar answered Sep 27 '22 17:09

unutbu