1
$\begingroup$

I have two different dataframes, both containing a common element (identifier). I want to copy data from df1 for each identifier in df2 (each identifier can repeat maximum 5 times in df1)

df1 ---data vertically

id_column         value1
identifier1       some data1     
identifier1       some data2
identifier1       some data3 
identifier2       some data2 
identifier2       some data1
identifier3       some data3

While df2 looks like this and this is how the information should be copied from df1:

df2 ---data horizontally

id_column        new1          new2           new3 

identifier1      some data1      some data2       some data3
identifier2      some data2      some data1 
identifier3      some data3

Is there a way to do this with python?

Many thanks for any help

$\endgroup$
1
$\begingroup$

If you want to add the data from df1 to df2 you can use pandas.merge. Given that you want to keep all records from df2 and only add data from df1 for the identifiers in df2 you can use the following syntax:

import pandas as pd

pd.merge(df2, df1, how="left", on="id_column")
$\endgroup$
2
  • $\begingroup$ thank you. I want to add data under specific columns but I don't know how to do that since the values under id_column are repeating in df1 - data is vertically while in df2 data is horizontally as in the example above $\endgroup$ – Isa Jan 28 at 13:53
  • $\begingroup$ You can use pandas.melt to convert data from wide to long or pandas.pivot to reshape your data even more freely. $\endgroup$ – Oxbowerce Jan 28 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.