Reputation: 1
How can I build a pandas pivot table using data from two different dataframes?
Table 1: Data frame 1
A B C D
1 apple 100 qwerty
2 apple 101 qwerty1
3 apple 102 qwerty2
Table 2:
A B C D
1 orange 200 asdfgh1
2 orange 201 asdfgh2
3 orange 202 asdfgh3
Pivot table goal:
C D
A B
1 apple 100 qwerty
orange 200 asdfgh1
2 apple 101 qwerty1
orange 201 asdfgh2
Upvotes: 0
Views: 468
Reputation: 758
import pandas as pd
df = pd.DataFrame({'A': [1,2,3], 'B': ['apple']*3 , 'C': [100, 101, 102], 'D': ['qwerty', 'qwerty1', 'qwerty2']})
df2 = pd.DataFrame({'A': [1,2,3], 'B': ['orange']*3 , 'C': [200, 201, 202], 'D': ['asdfgh', 'asdfgh1', 'asdfgh2']})
pd.concat((df,df2)).groupby(['A', 'B']).first()
C D
A B
1 apple 100 qwerty
orange 200 asdfgh
2 apple 101 qwerty1
orange 201 asdfgh1
3 apple 102 qwerty2
orange 202 asdfgh2
Upvotes: 2