Sayed Gouda
Sayed Gouda

Reputation: 615

merging multiple data sets with similar columns names

I have a multiple data sets represents multiple economic indicators. Every data set have 5 columns with the same columns names for every data set. The columns names are [Date Time, Actual, Consensus, Previous, Revised]. The thing is I want to merge these data sets into a single one to prepare it for future work. I did try this:

import pandas as pd

df1 = pd.read_csv(r'E:\Tutorial\Sentix Investor Confidence - European Monetary Union.csv')
df2 = pd.read_csv(r'E:\Tutorial\Services Sentiment - European Monetary Union.csv')
df3 = pd.read_csv(r'E:\Tutorial\ZEW Survey - Economic Sentiment - European Monetary Union.csv')
frames = [df1, df2, df3]
result = pd.concat(frames,join='inner')
print(result)

But the result is like this data result Which is absolutely wrong for me because despite similarity in names it’s extremely different indictors so I can NOT just mix them together. What I need is some thing like this, or some other thing do similar job to remain every indictor with its identity.

Upvotes: 0

Views: 40

Answers (1)

BENY
BENY

Reputation: 323356

Still using pd.concat

pd.concat([df,df,df],keys=['yourkey1','yourkey2','yourkey3'],axis=1)
Out[234]: 
  yourkey1     yourkey2     yourkey3    
        C1  C2       C1  C2       C1  C2
0        1  10        1  10        1  10
1        2  20        2  20        2  20
2        3   3        3   3        3   3
3        4  40        4  40        4  40

Data input

df = pd.DataFrame({'C1': [1,2,3,'4'], 'C2': [10, 20, '3',40]})

change the 'yourkey_' to the one you need

Upvotes: 1

Related Questions