【问题标题】:Iterate through columns and divide column value and creating new column with result in column遍历列并划分列值并在列中创建新列
【发布时间】: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 输出

标签: python pandas dataframe


【解决方案1】:

为什么不直接使用cut

In [12]: df = pd.DataFrame({'age':[5,8,10,25,33,22,38]})

In [13]: df
Out[13]:
   age
0    5
1    8
2   10
3   25
4   33
5   22
6   38

In [14]: df['age_group'] = pd.cut(df.age,bins=[5,10,15,20,25,30,35,40])

In [15]: df
Out[15]:
   age age_group
0    5       NaN
1    8   (5, 10]
2   10   (5, 10]
3   25  (20, 25]
4   33  (30, 35]
5   22  (20, 25]
6   38  (35, 40]

In [16]: df.groupby(['age_group']).count()
Out[16]:
           age
age_group
(5, 10]      2
(10, 15]     0
(15, 20]     0
(20, 25]     2
(25, 30]     0
(30, 35]     1
(35, 40]     1

【讨论】:

    【解决方案2】:

    df['{}'.format(n)].iloc = df[s].iloc/5 这一行完全错误:

    • s 不是列名,而是一对 (column_name, column),因此它不能用于索引数据帧。
    • iloc 也是错的,反正也没用
    • 非常改变你正在迭代的东西是很危险的,所以在for s in df.iteritems():循环中,你永远不应该向df添加列

    根据预期的结果,你想要的是:

    cols = df.columns.tolist()
    for j, s in enumerate(cols):  # ok cols is a plain list
        for i in range(5):
            df[str(i + 5*j)] = df[s]/5
    

    【讨论】:

      猜你喜欢
      • 2021-04-14
      • 2021-02-21
      • 1970-01-01
      • 2016-11-28
      • 2015-12-11
      • 2015-09-21
      • 2015-11-14
      • 2020-06-18
      • 1970-01-01
      相关资源
      最近更新 更多