python 3.x - Pandas: Join multiple data frame on the same keys -
i need join 5 data frames using same key. created several temporary data frame while doing join. code below works fine, wondering there more elegant way achieve goal? thanks!
df1 = pd.read_pickle('df1.pkl') df2 = pd.read_pickle('df2.pkl') df3 = pd.read_pickle('df3.pkl') df4 = pd.read_pickle('df4.pkl') df5 = pd.read_pickle('df5.pkl') tmp_1 = pd.merge(df1, df2, how ='outer', on = ['id','week']) tmp_2 = pd.merge(tmp_1, df3, how ='outer', on = ['id','week']) tmp_3 = pd.merge(tmp_2, df4, how ='outer', on = ['id','week']) result_df = pd.merge(tmp_3, df5, how ='outer', on = ['id','week'])
use pd.concat
after setting index
dfs = [df1, df2, df3, df4, df5] cols = ['id', 'weedk'] df = pd.concat([d.set_index(cols) d in dfs], axis=1).reset_index()
include file reading
from glob import glob def rp(f): return pd.read_pickle(f).set_index(['id', 'week']) df = pd.concat([rp(f) f in glob('df[1-5].pkl')], axis=1).reset_index()
Comments
Post a Comment