0

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:

Data frame 2

A     B      C    D
1   orange  200 asdfgh1
2   orange  201 asdfgh2
3   orange  202 asdfgh3

Pivot table goal:

Goal Pivot table

                    C         D
A         B
1       apple      100      qwerty
        orange     200      asdfgh1
2       apple      101      qwerty1
        orange     201      asdfgh2
1
  • 1
    df1.append(df2) ? Commented Sep 30, 2021 at 4:22

1 Answer 1

2
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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.