Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何修复迭代过程中的ValueError?_Python 3.x_Iteration_Pandas Groupby - Fatal编程技术网

Python 3.x 如何修复迭代过程中的ValueError?

Python 3.x 如何修复迭代过程中的ValueError?,python-3.x,iteration,pandas-groupby,Python 3.x,Iteration,Pandas Groupby,由于值错误,无法迭代dataframe的分组值 我考虑的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

由于值错误,无法迭代dataframe的分组值

<>我考虑的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
为了将列(即“子”和“标记”)的值分组为列表,我使用

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)
下面是一个示例,它将返回第一列数据


groupby方法已返回一个数据帧,您无法再次循环它。

我已尝试使用for index,group in data\u df\u grp.groupby(['class','section','school','city']):&for index,row in group.iterrows():您可以定义
data\u df\u grp
?请参阅“”@amannagariya,我也尝试过,它返回了ValueError
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)