Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add values from different dataframes

df1:
            A    B
0  2002-01-13  3.9
1  2002-01-13  1.9
2  2002-01-14  8.0
3  2002-01-14  9.0

I want to create a new column df1["C"] with means of B values, per each A group.

Output should be:

            A    B     C
0  2002-01-13  3.9   2.9
1  2002-01-13  1.9   2.9
2  2002-01-14  8.0   8.5
3  2002-01-14  9.0   8.5

And now I want to assign C values to each A group, to another df2.

df2:
            A        D
0  2002-01-13   Joseph
1  2002-01-13     Emma
2  2002-01-13  Michael
3  2002-01-14     Anna
4  2002-01-14   Yvonne
5  2002-01-14  Anthony

Output should be:

            A        D     E
0  2002-01-13   Joseph   2.9
1  2002-01-13     Emma   2.9
2  2002-01-13  Michael   2.9
3  2002-01-14     Anna   8.5
4  2002-01-14   Yvonne   8.5
5  2002-01-14  Anthony   8.5

I´ve tried:

df1["C"] = df1.groupby("A")["B"].mean()
like image 770
Tie_24 Avatar asked Feb 22 '26 00:02

Tie_24


2 Answers

You don't have to add a column to df1, you can directly map the values from the groupby df1 to df2.

df2['E'] = df2['A'].map(df1.groupby('A').B.mean())


    A           D       E
0   2002-01-13  Joseph  2.9
1   2002-01-13  Emma    2.9
2   2002-01-13  Michael 2.9
3   2002-01-14  Anna    8.5
4   2002-01-14  Yvonne  8.5
5   2002-01-14  Anthony 8.5
like image 198
Vaishali Avatar answered Feb 23 '26 15:02

Vaishali


First question transform

df1['C'] = df1.groupby('A').B.transform('mean')

Second using map (Notice I am using the df1 directly cause I adding drop_duplicates )

df2['E']=df2.A.map(df1.drop_duplicates('A').set_index('A').C)
like image 21
BENY Avatar answered Feb 23 '26 14:02

BENY



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!