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()

```
If I understand your question correctly, You could simply do - 

```
data_df.groupby('Market').agg({'Rooms': np.sum}) OR 

data_df.groupby(['market'], as_index=False).agg({'Rooms': np.sum})

data_df = pd.DataFrame({'Market' : ['A','B','C','B'],
                    'Hotel'  : ['H1','H2','H4','H5'],
                    'Rooms'  : [20,40,50,34]
                  })

data_df.groupby('Market').agg({'Rooms': np.sum})

```

[![Results](https://i.stack.imgur.com/dGLxn.png)](https://i.stack.imgur.com/dGLxn.png)