I have a dataframe in python, which I want to perform a simply aggregation on. My df looks like:
df:
City | Sex | Age |
---|---|---|
London | Male | 20 |
London | Female | 30 |
London | Male | 25 |
London | Female | 22 |
London | Male | 33 |
London | Female | 45 |
I want to return a dataframe still with 3 columns but the minimum age, so the result I'm looking for would be:
City | Sex | Age |
---|---|---|
London | Male | 20 |
London | Female | 22 |
I've used:
df = df.groupby(['City', 'Sex']).min()
but this just returns a df of:
Age |
---|
20 |
22 |
How do I keep the group by columns in the revised dataframe? I see the concatenated columns as the index but the df.info is just the one column, as it also is if I output it to excel.
Thanks