0

I tried to implement the solution provided here and I am getting "invalid syntax" I am trying to get the max value + firs/last value.

and my code looks like this:

groups = df[df['isTrade'] == 1].groupby('dateTime_s')                         
print(groups.agg({
      'Volume': np.sum,
      'tradePrice':[np.max,lambda x: x.iloc[0]], 
      }).head(160))

Thanks for your help!

Joe
  • 12,057
  • 5
  • 39
  • 55
Giladbi
  • 1,822
  • 3
  • 19
  • 34

1 Answers1

1

It seems you need GroupBy.first / GroupBy.last:

df = pd.DataFrame({'tradePrice':[7,8,9,4,2,3],
                   'Volume':[1,3,5,7,1,0],
                   'isTrade':[2,1,1,1,2,4],
                   'dateTime_s':list('aaabbb')})

print (df)
   Volume dateTime_s  isTrade  tradePrice
0       1          a        2           7
1       3          a        1           8
2       5          a        1           9
3       7          b        1           4
4       1          b        2           2
5       0          b        4           3

groups = df[df['isTrade'] == 1].groupby('dateTime_s')  
print(groups.agg({
      'Volume': 'sum',
      'tradePrice':['max','first'], 
      }).head(160))

           tradePrice       Volume
                  max first    sum
dateTime_s                        
a                   9     8      8
b                   4     4      7
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252