Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

question from:https://stackoverflow.com/questions/65928030/copy-data-between-two-dataframes-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
258 views
Welcome To Ask or Share your Answers For Others

1 Answer

I am not sure I understand, but if you want to convert df1 into df2, you can use:

df1.pivot(index='id',columns='value1')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...