2

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

Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
  • 1
    A groupby is a lazily evaluated object that doesn't have a to_csv method. You need to take your groupby object and apply, transform, or aggregate it to make it a dataframe that DOES have a to_csv method as in [this question](https://stackoverflow.com/questions/47602097/pandas-groupby-to-to-csv) – G. Anderson May 22 '19 at 17:34

1 Answers1

1

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")
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74