I am troubling with counting the number of counties using famous cenus.csv data.
Task: Count number of counties in each state.
Facing comparing (I think) / Please read below?
I've tried this:
df = pd.read_csv('census.csv')
dfd = df[:]['STNAME'].unique() //Gives out names of state
serr = pd.Series(dfd) // converting to series (from array)
After this, i've tried using two approaches:
1:
df[df['STNAME'] == serr] **//ERROR: series length must match**
2:
i = 0
for name in serr: //This generate error 'Alabama'
df['STNAME'] == name
for i in serr:
serr[i] == serr[name]
print(serr[name].count)
i+=1
Please guide me; it has been three days with this stuff.
Use
groupby
and aggregateCOUNTY
usingnunique
:Now the results
juanpa.arrivillaga has a great solution. However, the code needs a minor modification.
The "counties" with
'SUMLEV' == 40
or'COUNTY' == 0
should be filtered. Otherwise, all the number of counties are too big by one.So, the correct answer should be:
with the following result:
@Bakhtawar - This is a very simple way: