How to solve AttributeError: 'DataFrameGroupBy' object has no attribute 'sort_values'

import numpy as np
import pandas as pd
data=pd.read_excel(r'DataScience.xls')
grouped=data.groupby(['class'])
print(grouped.sort_values(by=['week'],ascending=[False]))

Don’t write your code like this

Write it using this `(3 times) and end with ` (3 times)
Like this

Your code here

[squote=“Zengcurry, post:1, topic:18193, full:true”]

import numpy as np
import pandas as pd
data=pd.read_excel(r’DataScience.xls’)
grouped=data.groupby([‘class’])
print(grouped.sort_values(by=[‘week’],ascending=[False]))

[/quote]

Yes write it like this:

```py
# Your code here
```
1 Like

` start and end both with 3 times

1 Like

That works for small snippets but not for larger snippets with indentation

1 Like

Ok apart from that I can explain the error but try to fix it alone. grouped is a grouped object, and sort_values() method cannot be applied to it. Instead, it should be applied to the result of, for example, an aggregation function applied to the grouped object.

4 Likes