6

I have a dataframe that has 3 columns -

enter image description here

I want to group the columns on the basis of Chopstick Length by doing something like this -

meansByCL = df_chopstick.groupby('Chopstick.Length')['Food.Pinching.Efficiency'].mean().reset_index()

but this throws an error -

AttributeError: Cannot access callable attribute 'groupby' of 'DataFrameGroupBy' objects, try using the 'apply' method

I'm not sure what this error means. Can anyone tell me what I'm doing wrong or how I can write this code differently?

4
  • What is your df_chopstick, looks like it is already a groupby object Commented Nov 21, 2018 at 6:08
  • @KevinFang df_chopstick is the image attached. It has Food.Pinching.Efficiency for different chopstick lengths(180-240) and different individuals (1-31). Commented Nov 21, 2018 at 6:12
  • 2
    Please double check whether it is modified elsewhere. I can reproduce this error when I call df.groupby().groupby() Commented Nov 21, 2018 at 6:16
  • Never mind! Found what I was doing wrong. I had assigned df_chopstick = df_chopstick.groupby('Chopstick.Length') in a previous cell of Jupyter notebook. Commented Nov 21, 2018 at 6:16

2 Answers 2

7

This happens when you are trying to groupby() a dataframe which has been already grouped before!

Sign up to request clarification or add additional context in comments.

2 Comments

...and the solution is to ungroup it with unstack() or reset_index()
unstack /reset_index when? before? after? during?
0

For my case, I have done a groupby and it was not required. I needed to use original dataset and just Index it (not groupby it). So remove the groupby and use below to set_index.
Yeah the problem is that groupby is already done and you can't groupby it again.

df.set_index(['ColA','ColB'])

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.