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

Categories

0 votes
152 views
in Technique[技术] by (71.8m points)

Pivot and copy data between two dataframes in python

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        pasted1          pasted2           pasted3 
identifier1      some data1      some data2       some data3
identifier2      some data2      some data1 
identifier3      some data3

There are many other columns in df1, that have a correspondent column in df2 and whose information need to be copied in df2 considering the identifier.

If I use this, creates the pivot but I want to be able to specify the columns name in df2, exact place/columns where data should be pasted (and do that for other columns too):

df2 = pd.pivot_table(df, index = 'id_column', columns = 'value1', aggfunc=lambda x: x['value1'][x.index[0]])
question from:https://stackoverflow.com/questions/65865340/pivot-and-copy-data-between-two-dataframes-in-python

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

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...