Question: I have a panda dataframe in Python at which I am applying a groupby. And then I want to apply a new groupby + sum on the previous result. To be more specific, first I am doing:

```
  check_df = data_df.groupby(['hotel_code', 'dp_id', 'market', 'number_of_rooms'])
[['market', 'number_of_rooms']]

```

And then I want to do:

```
check_df = check_df.groupby(['market'])['number_of_rooms'].sum()

```

So, I am getting the following error:

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

```

My initial data look like that:

```
hotel_code | market | number_of_rooms | ....
---------------------------------------------
001        |   a    |      200        | ... 
001        |   a    |      200        |
002        |   a    |      300        | ...

```

Notice that I may have duplicates of pairs like (a - 200), that's why I want need the first groupby.
What I want in the end is something like that:

```
Market | Rooms
--------------
a      | 3000
b      | 250

```

I'm just trying to translate the following sql query into python:

```
select a.market, sum(a.number_of_rooms)
from (
        select market, number_of_rooms
        from opinmind_dev..cg_mm_booking_dataset_full
        group by hotel_code, market, number_of_rooms
        ) as a
group by market ;

```

Any ideas how I can fix that? If you need any more info, let me know.

ps. I am new to Python and data science

Answer: 
IIUC, instead of:

```
check_df = data_df.groupby(['hotel_code', 'dp_id', 'market', 'number_of_rooms'])
[['market', 'number_of_rooms']]

```

You should simply do:

```
check_df = data_df.drop_duplicates(subset=['hotel_code', 'dp_id', 'market', 'number_of_rooms'])\
                  .loc[:, ['market', 'number_of_rooms']]\
                  .groupby('market')\
                  .sum()

```
```
df = pd.DataFrame({'Market': [1,1,1,2,2,2,3,3], 'Rooms':range(8), 'C':np.random.rand(8)})

      Market  Rooms         C
0       1      0  0.187793
1       1      1  0.325284
2       1      2  0.095147
3       2      3  0.296781
4       2      4  0.022262
5       2      5  0.201078
6       3      6  0.160082
7       3      7  0.683151

```

You need to move the column selection away from the grouped DataFrame. Either of the following should work. 

```
df.groupby('Market').sum()[['Rooms']] 
df[['Rooms']].groupby(df['Market']).sum()
         Rooms
Market       
1           3
2          12
3          13

```

If you select using `['Rooms']` instead of `[['Rooms']]` you will get a Series instead of a DataFrame.

The dataframes produced use market as their index. If you want to convert it to a normal data column, use:

```
df.reset_index()
   Market  Rooms
0       1      3
1       2     12
2       3     13

```