I have a dataframe pasted below with two-level indexing (first level is statename and second level is county name): the image contains the statename and the counties in it in the second column
I need to count the number of counties in each state. (In the image pasted i have just information for a single state but the entire dataframe consists of several such similar entries for states and corresponding county names.) for ex. in the above pic it should return 5 for Alabama (i know there are 60 other counties within alabama but i have just pasted a small part of the bigger dataframe). Which pandas function can be used to return the counties count for each state? I am a novice to pandas and just in the learning stage.
thanks for the help in advance.
I think you need groupby
and aggregate by size
:
df1 = df.groupby(level=0).size()
Or:
df1 = df.groupby('STNAME').size()
Sample:
df = pd.DataFrame({'STNAME':['AL'] * 3 + ['MI'] * 4,
'CTYNAME':list('abcdefg'),
'COL': range(7) }).set_index(['STNAME','CTYNAME'])
print (df)
COL
STNAME CTYNAME
AL a 0
b 1
c 2
MI d 3
e 4
f 5
g 6
df1 = df.groupby(level=0).size().reset_index(name='count')
print (df1)
STNAME count
0 AL 3
1 MI 4
df1 = df.groupby('STNAME').size().reset_index(name='count')
print (df1)
STNAME count
0 AL 3
1 MI 4
it will give count for each st_name
. i experimented with small list
df = pd.DataFrame({'st_name': 'alabama', 'cityname': [['alabama'], ['autuguva','county'],['county']]})
df.groupby('st_name').count()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.