News

New paper!

Wednesday, December 13, 2017

Concatenate several DataFrames (sorted with the index, add new columns)

pd.concat([df list], axis=1) makes a new DataFrames in which each column is sorted with the index


>>> df1 = pd.DataFrame([[1],[2]],columns=['number'],index=['f1','f2'])
>>> df1
   number
f1       1
f2       2

>>> df2 = pd.DataFrame([['b'],['a']],columns=['letter'],index=['f2','f1'])
>>> df2
  letter
f2      b
f1      a

>>> pd.concat([df1,df2],axis=1)
   number letter
f1       1      a
f2       2      b

*Note the order of rows was automatically sorted to match the index.
If axis=0, df2 will be added as new rows.