I am trying to write a for loop that will take a dataframe with census data, count the populations of the three largest counties of each state, and write the sum to a new series. Here is the function that is not working:
import numpy as np
import pandas as pd
##created a dataframe earlier with a census csv file called 'census_df'
def bad_function():
only_counties = census_df.set_index(['STNAME'])
ser = pd.Series(index = only_counties.index)
ser = ser.index.drop_duplicates() ##get a unique list of all 50 states from the dataframe
state_name = pd.Series(index = ser)
for i in state_name.index:
a = only_counties.loc[i, 'CENSUS2010POP']
a = a.sort_values(ascending=False)
population = np.sum(a[0:3])
state_name.loc[i] = population
return state_name
When I call this function, I get the following error:
AttributeError Traceback (most recent call last)
<ipython-input-59-dc2686648261> in <module>()
26 return state_name
27
---> 28 answer_six()
<ipython-input-59-dc2686648261> in answer_six()
18 for i in state_name.index:
19 a = only_counties.loc[i, 'CENSUS2010POP']
---> 20 a = a.sort_values(ascending=False)
21
22 population = np.sum(a[0:3])
AttributeError: 'numpy.int64' object has no attribute 'sort_values'
HOWEVER, when I ditched the loop for testing purposes and selected one item('Alabama') from the index of what I am trying to iterate over, and use the same sort_values method in the same way, it works just fine. Like this:
def bad_function():
only_counties = census_df.set_index(['STNAME'])
ser = pd.Series(index = only_counties.index)
ser = ser.index.drop_duplicates()
state_name = pd.Series(index = ser)
a = only_counties.loc['Alabama', 'CENSUS2010POP']
a = a.sort_values(ascending=False)
b = np.sum(a[0:3])
return a, b
It returns exactly what I want, which is a: a list of counties in the state sorted by population and b: the sum of the three highest population counties. So what is happening?
Are you the following:
for i in state_name.index:
print (I)
prints the state names or index?
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.