python-如何在迭代熊猫分组df中修复ValueError?

时间:2019-04-04 06:05:38

标签: python-3.x iteration pandas-groupby

由于值错误,无法迭代熊猫数据框的分组值。

我考虑的df是

df:
  class section  sub  marks school   city
0     I       A  Eng     80  jghss  salem
1     I       A  Mat     90  jghss  salem
2     I       A  Eng     50  jghss  salem
3   III       A  Eng     80  gphss  salem
4   III       A  Mat     45  gphss  salem
5   III       A  Eng     40  gphss  salem
6   III       A  Eng     20  gphss  salem
7   III       A  Mat     55  gphss  salem

我将列的值(即“ sub”和“ marks”)分组为列表,

df_grp = df.groupby(['class','section','school','city']).agg(lambda x: list(x))

df_grp是

class section school city    sub                       marks                                                
I     A       jghss  salem            [Eng, Mat, Eng]          [80, 90, 50]
III   A       gphss  salem  [Eng, Mat, Eng, Eng, Mat]  [80, 45, 40, 20, 55]

现在我需要迭代df_grp,以便提取所有列的值,例如

Row 1:-
    class = I
    section = A
    school = jghss
    city = salem
    sub = [Eng, Mat, Eng]
    marks = [80, 90, 50]

Row 2:-
    class = III
    section = A
    school = gphss
    city = salem
    sub = [Eng, Mat, Eng, Eng, Mat]
    marks = [80, 45, 40, 20, 55]

现在要迭代df_grp以提取列值,我已经使用

for index,group in df_grp:
    for subIndex, row in group.iterrows():
        sub = row['sub']
        marks = row['marks']

当我使用它时,它会返回

ValueError: too many values to unpack (expected 2)

1 个答案:

答案 0 :(得分:2)

import pandas as pd

df1 = pd.DataFrame({
    'atable':     ['Users', 'Users', 'Domains', 'Domains', 'Locks'],
    'column':     ['col_1', 'col_2', 'col_a', 'col_b', 'col'],
    'column_type':['varchar', 'varchar', 'int', 'varchar', 'varchar'],
    'is_null':    ['No', 'No', 'Yes', 'No', 'Yes'],
})

df1_grouped = df1.groupby('atable').agg(lambda x: list(x))
for row in df1_grouped.iterrows():
    print(row[1].column)

这里是一个示例,它将返回第一列数据

groupby方法已经返回了数据帧,您无法再次循环。