Issue
I have two dataframes df1
and df2
like the following ones
df1
val
0 -2
1 -1
2 -5
3 -2
4 -4
5 -7
6 -7
7 -3
8 -6
df2
Name Year size
0 Marc 2010 2
1 Marc 2011 1
2 Eric 2010 1
3 Eric 2011 1
4 Sara 2010 2
5 Sara 2011 2
I would like to update the information of df1
based on the information from df2
in the following way:
df1
val Name Year
0 -2 Marc 2010
1 -1 Marc 2010
2 -5 Marc 2011
3 -2 Eric 2010
4 -4 Eric 2011
5 -7 Sara 2010
6 -7 Sara 2010
7 -3 Sara 2011
8 -6 Sara 2011
P.S. I can't join the information.
Solution
Try:
>>> df1.join(df2.reindex(df2.index.repeat(df2["size"])).reset_index(drop=True)).drop("size", axis=1)
Val Name Year
0 -2 Marc 2010
1 -1 Marc 2010
2 -5 Marc 2011
3 -2 Eric 2010
4 -4 Eric 2011
5 -7 Ssara 2010
6 -7 Ssara 2010
7 -3 Ssara 2011
8 -6 Ssara 2011
Answered By - not_speshal
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.