A B C
0 Bob 10 2
1 Bob 11 8
2 Sarah 23 -2
3 Sarah 24 4
4 Jack 19 -4
5 Jack 21 -1
I want to get a new df["Point"] as follows:
- To "Bob" group:
df["Point"]is the multiplication of first B value by C values. 10*2=20; 10*8=80. - To "Sarah" group:
df["Point"]is the multiplication of first B value by C values. 23*(-2)=(-46); 23*4=92. - To "Jack" group:
df["Point"]is the multiplication of first B value by C values. 19*(-4)=(-76); 19*(-1)=(-19).
I mean, I want to get:
A B C Point
0 Bob 10 2 20
1 Bob 11 8 80
2 Sarah 23 -2 -46
3 Sarah 24 4 92
4 Jack 19 -4 -76
5 Jack 21 -1 -19
After that, I want to do the following iteration:
results = {}
grouped = df.groupby("A")
for idx, group in grouped:
if (group["Point"] > 50).any():
results[idx] = group[group["Point"] > 50].head(1)
print ("")
else:
results[idx] = group.tail(1)
print ("")
print(results[idx])
And get this results:
A B C Point
1 Bob 11 8 80
A B C Point
3 Sarah 23 4 92
A B C Point
5 Jack 21 -1 -19
I guess I have to do a double iteration but I don´t know how, or if it possible to do that in a different way.
Sarahgroup multiple by second value?