Mahamutha M
Mahamutha M

Reputation: 1287

How to fix ValueError in iterating pandas grouped df?

Unable to iterate the grouped value of pandas dataframe because of value error.

The df which I have consider is,

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

For grouping the values of columns(i.e., "sub" & "marks") as list, I have used,

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

The df_grp is,

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]

Now I need to iterate the df_grp, in order to extract the values of all the columns like

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]

Now to iterate the df_grp to extract the column values, I have used

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

When I use the same it returns

ValueError: too many values to unpack (expected 2)

Upvotes: 3

Views: 430

Answers (1)

aman nagariya
aman nagariya

Reputation: 172

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)

Here is an example, it will return the first column data

groupby method already returning a dataframe and you cannot loop it again.

Upvotes: 2

Related Questions