Understanding Merge with Different Label Names

Asked 2 years ago, Updated 2 years ago, 42 views


as shown in the table below.
Trying to combine two data frames with different label names in the same sense If so, what should I do?

Normally, the answer would be as follows...
pd.merge(A,B,on="Date")

By the way, the date and date are defined as indexes.

A
    date price
2000-01-05 100
2000-01-06 110 
2000-01-07 120

B
    Date price
2000-01-05 50
2000-01-06 60
2000-01-08 70

python python3 pandas

2022-09-30 19:29

2 Answers

"About merge when label names are different" ラベル Match label names!I don't think it's an answer, but
If you want the results, you can do it using the following method.
Change the index and column names to merge with concat.

import pandas as pd
dateA=pd.DatetimeIndex (['2000-01-05', '2000-01-06', '2000-01-07', name='Date')
dateB=pd.DatetimeIndex (['2000-01-05', '2000-01-06', '2000-01-08', name='Date')

A=pd.DataFrame ({'Price':[100, 110, 120]}, index=dateA)
B=pd.DataFrame({'price':[50,60,70]},index=dateB)

tmp = B.rename_axis('date') # Rename index
Rename tmp=tmp.rename(index=str,columns={'price':'price'})#column
Merge with tmp=pd.concat([A,tmp], ignore_index=False)#concat
AB = tmp

The results are included in AB, but the date is the same, so I will add the following

 tmp['date'] = tmp.index#A and B have overlapping dates, so if the price is different, it will be difficult to handle if you are in index.
tmp.index=range(len(tmp.index))#Excuse date to column
tmp = tmp [['date', 'price']] # column reorder
AB = tmp.sort_values(by = ['date'], ascending=True)# Sort by date

I don't want to write this kind of code, so I think I asked you how to do it with one operation.
You can merge using this until you get a smart answer.


2022-09-30 19:29

By the way, the date and date are defined as indexes.

If that's the case, wouldn't we just pass left_index=True and right_index=True to the parameters?

import pandas as pd
df_A = pd.DataFrame({'Price':[100, 110,120]},
                    index=pd.Index(pd.date_range('20000105', periods=3, freq='d'), name='date'))
df_B = pd.DataFrame({'price':[50,60,70]},
                    index=pd.Index(pd.date_range('20000105', periods=3, freq='d'), name='Date'))
print(pd.merge(df_A,df_B,left_index=True,right_index=True))
#             price
# date
#2000-01-05  100     50
#2000-01-06  110     60
#2000-01-07  120     70


2022-09-30 19:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.