I'm working with census data, and the columns provide age groups instead of discrete ages. For the sake of working with the data on a generational basis (millennials, baby boomers, etc), I need to convert these to to single years. (i.e. One column for Ages 5-9 instead of one column for Age 5, one column for Age 6, etc.)
As I'm just trying to identity trends, I'm fine with just splitting each age category equally to get a discrete value for each year.
I'd like to iterate through each column, and create 5 new columns, each with the original column value divided by 5 (as there are 5 years in each group.
I've tried setting variables at zero for both the new column names and the index of the column I want to divide equally.
I've then written a for loop to iterate through each column in the dataframe. Within that I have a nested for loop so it performs the operation 5 times on each column. I'm then incrementing the n value for the new column name each time in the inner for loop, and incrementing the s value for the index of the column being divided in the outer for loop.
df = pd.DataFrame([[6.8, 6.5], [5.2, 8.9], [6.4, 7.6]], columns= ['Under 5 years', '5 to 9 years'])
## Set up variables. 'n' is for the new column name. 's' is the index of the column to be divided.
n= 0
s = 0
## For loop to iterate through each column in the dataframe and perform the operation on each column 5 times before moving onto the next column:
for s in df.iteritems():
for i in range(5):
df['{}'.format(n)].iloc = df[s].iloc/5
n+=1
s+=1
I keep getting a Type Error: Under 5 years, dtype: float64)' is an invalid key
I can't figure out how to resolve this error, or if the rest of the code will even work properly.
Desired Output would be the following dataframe:
df = pd.DataFrame([[6.8, 6.5, 1.36, 1.36, 1.36, 1.36, 1.36, 1.3, 1.3, 1.3, 1.3, 1.3], [5.2, 8.9, 1.04, 1.04, 1.04, 1.04, 1.04, 1.78, 1.78, 1.78, 1.78, 1.78], [6.4, 7.6, 1.28, 1.28, 1.28, 1.28, 1.28, 1.52, 1.52, 1.52, 1.52, 1.52]], columns=['Under 5 years', '5 to 9 years', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'])