Reputation: 8586
I have a Dataframe in Pandas that shows percentage of men in a city/state. The dataframe df
looks like the following (note this is not my actual usage/data but my datatypes are similar)
STATE CITY PERC_MEN
ALABAMA ABBEVILLE 41.3%
ALABAMA ADAMSVILLE 53.5%
....
WYOMING WRIGHT 46.6%
Each State/percentage of men combo will have exactly 1 value returned.
How do I show the city/population values for a given state? My code looks like the following (I need the first line where I groupby STATE
because I do other stuff with the data)
for state, state_df in df.groupby(by=['STATE']):
print(state_df.groupby(by=['CITY'])['PERC_MEN'])
However this prints <pandas.core.groupby.generic.SeriesGroupBy object at 0xXXXXXXX>
Normally for groupby's I use an aggregate like mean()
or sum()
but is there a way to just return the value?
Upvotes: 1
Views: 186
Reputation: 26676
I wouldnt iterate dataframe.
Set index and slice
df=df.set_index(['STATE','CITY'])
df.xs(('ALABAMA', 'ABBEVILLE'), level=['STATE','CITY'])
or
df.loc[('ALABAMA', 'ABBEVILLE'),:]
Upvotes: 1