Reputation:
I have a df where I need to make a calculation. volume.shift(1) + added volume - volume = total volume This works for me when I have only one product id now I want to apply this on a df with multiple product id's. Plus I need to generate a csv when I'm done. I tried a group by but calculations don't seem to be adding up and I can't output a csv. I get this error:
Cannot access callable attribute 'to_csv' of 'DataFrameGroupBy' objects, try using the 'apply' method
when I have one product ID this code makes the correct calculations:
df['TOTAL_VOLUME'] = df['VOLUME'].shift(-1) + df['ADDED_VOLUME'] - df['VOLUME']
df = data.groupby('PRODUCT_ID')
df['TOTAL_VOLUME'] = df['VOLUME'].shift(-1) + df['ADDED_VOLUME'] - df['VOLUME']
df.to_csv(r"C:\Desktop\total volume.csv")
What I would like to do is make the total volume calculation but only when the PRODUCT_ID matches. I'm trying to do this with by calling group by.
My biggest issue is creating a csv after group by is called.
Cannot access callable attribute 'to_csv' of 'DataFrameGroupBy' objects, try using the 'apply' method
Upvotes: 2
Views: 91
Reputation: 150735
I would update the original data frame and save it instead:
data['shifted_volumn'] = data.groupby('PRODUCT_ID')['VOLUME'].shift()
data['TOTAL_VOLUME'] = data['shifted_volumn'] + data['ADDED_VOLUME'] - data['VOLUME']
data.to_csv(r"C:\Desktop\total volume.csv")
Upvotes: 1