Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterations over dataframe groupby

Tags:

python

pandas

       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.

like image 998
Tie_24 Avatar asked Sep 11 '26 03:09

Tie_24


1 Answers

For first create new column by transform with first and multiple by C column:

df['point'] = df.groupby('A')['B'].transform('first').mul(df['C'])
print (df)
       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

And then filter first all rows by condition and get only first rows by drop_duplicates - keep='first' is by default:

df1 = df[df['point'] > 50].drop_duplicates('A')
print (df1)
       A   B  C  point
1    Bob  11  8     80
3  Sarah  24  4     92

Then filter rows which are not in df1.A column by isin and inverted condition by ~, again drop_duplicates with keep last rows only:

df2 = df[~df['A'].isin(df1['A'])].drop_duplicates('A', keep='last')
print (df2)
      A   B  C  point
5  Jack  21 -1    -19

Last use concat with dict comprehension for final dictionary:

d = {k: v for k, v in pd.concat([df1, df2]).groupby('A')}
print (d)
{'Bob':      A   B  C  point
1  Bob  11  8     80, 'Jack':       A   B  C  point
5  Jack  21 -1    -19, 'Sarah':        A   B  C  point
3  Sarah  24  4     92}
like image 157
jezrael Avatar answered Sep 12 '26 16:09

jezrael



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!