Linked Questions

Popular Questions

pandas dataframes merging rows into columns

Asked by At

I have 2 pandas dataframes (df1, df2) that I am trying to extract data from and create a 3rd dataframe (df3)

df1 has 2 columns (an id column and another column that hold the name of the columns in the 2nd dataframe (df2)

df1 looks like:
===============
id1      name
---      ----
1        df2_column1_name
5        df2_column1_name
33       df2_column3_name
...
... and so on

df2 looks like:
===============
id2  df2_column1_name   df2_column2_name   df2_column2_name .... and so on
---  ----------------   ----------------   ----------------
12   Jimmy              male               25               .... 
16   Becky              female             30               ....
75   Mike               male               80               ....
....
.... and so on


I am trying to create df3 to look like:
=======================================
column1  Column2  Column3
-------  -------  -------
1        12       Jimmy    
5        12       male 
33       12       25
.
.
1        16       Becky
5        16       female
33       16       30
.
.
1        75       Mike
5        75       male
33       75       80
.
.
.

The dataframes can be quite large. I am trying to figure out the most efficient way to do this without double looping if possible. please advise best way to do this. Thank you

Related Questions