Column binding two Panda's Dataframes

Joining two Pandas DataFrames with an equal number of rows is slightly harder than it appears. In R, you just use the cbind function.

As this StackOverflow question shows, in Pandas it’s easy to end up with something like this:

1
2
3
4
5
6
7
unique_id lacet_number    latitude  longitude
0         NaN          NaN  -93.193560  31.217029
1         NaN          NaN  -93.948082  35.360874
2         NaN          NaN -103.131508  37.787609
15    5570613  TLA-0138365         NaN        NaN
24    5025490  EMP-0138757         NaN        NaN
36    4354431  DXN-0025343         NaN        NaN

This results from the indices not being identical. Frustratingly (to me) the ignore_index argument doesn’t give the 3-rowed DataFrame I’d hope it gives.

As the accepted answer on that question shows, the thing to do is reset the indices on the DataFrames before concatenating:

1
pd.concat([df_a.reset_index(drop=True), df_b.reset_index(drop=True)], axis=1)
Feedback
FOOTER