【发布时间】:2019-11-06 12:50:55
【问题描述】:
我正在处理人口普查数据,这些列提供了年龄组,而不是离散的年龄。为了在代际(千禧一代、婴儿潮一代等)的基础上处理数据,我需要将这些数据转换为单年。 (即 5-9 岁的一栏,而不是 5 岁的一栏,6 岁的一栏等)
由于我只是想确定趋势,我可以将每个年龄类别平均划分以获得每年的离散值。
我想遍历每一列,并创建 5 个新列,每个列的原始值除以 5(因为每组有 5 年。
我已经尝试将新列名和我想要平均划分的列的索引都设置为零。
然后我编写了一个 for 循环来遍历数据框中的每一列。其中我有一个嵌套的 for 循环,因此它在每列上执行 5 次操作。然后,我每次在内部 for 循环中递增新列名的 n 值,并在外部 for 循环中递增被划分列的索引的 s 值。
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
我不断收到类型错误:不到 5 年,dtype: float64)' 是无效键
我不知道如何解决这个错误,或者其他代码是否能正常工作。
所需的输出将是以下数据框:
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'])
【问题讨论】:
-
请为您的问题提供minimal reproducible example。请参阅有关创建 good pandas examples 的链接,以便我们知道如何更好地帮助您
-
@G.Anderson 完成。感谢您的链接。
-
根据提供的输入,您想要的输出是什么?你的问题并不完全清楚
-
什么是
df_numbers? -
@G.Anderson 已更新以包含所需的 df 输出