I'm wondering if I'm able to do the following in one line or if it's necessary to do it in two (I'm coming from R where I know how to do it in one call). I want to compute the batting average which requires manipulation of both the hits and the at bats columns
import pandas as pd
batting = pd.DataFrame({'playerID': [1, 1, 1, 2, 2, 2],
'h': [80, 97, 95, 30, 35, 22],
'ab': [400, 410, 390, 150, 170, 145]})
batters = (batting.groupby('playerID')
.agg({'h' : 'sum', 'ab' : 'sum'})
.reset_index())
batters['ba'] = batters['h']/batters['ab']