don't know why: AttributeError: 'list' object has no attribute 'groupby'

In my code, type(i) is str, still got the traceback below. Have not found the reason yet, neither a similar question, can anyone offer a help? thanks.

<class 'str'>
data = a.groupby(i)['income'].sum()
AttributeError: 'list' object has no attribute 'groupby'

the data is like this:

ta  tb    tc  ...   income
1   2011   a  ...   1352.23
1   2012   c  ...   706.87
.   .      .  ...   ...
.   .      .  ...   ...
.   .      .  ...   ...
2   2011   b  ...   3618.04
2   2012   c  ...   11745.42
3   2013   a  ...   345.98

the code:

df = pd.read_csv('e:/test_csv', low_memory=False)
a = ['ta', 'tb', 'tc']
for i in a:
    print(type(i))
    data = a.groupby(i)['income'].sum()
    data.plot.pie(autopct='%.1f%%')
plt.show()

At first, I had 3 similar codes for 3 pie chart, then i wonder if i can do it with only 1 code.

the code:

df = pd.read_csv('e:/test_csv', low_memory=False)

data1 = a.groupby('ta')['income'].sum()
data1.plot.pie(autopct='%.1f%%')

data2 = a.groupby('tb')['income'].sum()
data2.plot.pie(autopct='%.1f%%')

data3 = a.groupby('tc')['income'].sum()
data3.plot.pie(autopct='%.1f%%')

plt.show()

3 answers

  • answered 2018-11-08 07:26 Rarblack

    The correct usage of groupby in pandas is this:

    data.groupby(['col1', 'col2'])['col3'].sum()
    

    You should have had:

    data = df.groupby(['ta', 'tb', 'tc'])['income'].sum()
    

    Official Documentation to refer.

  • answered 2018-11-08 07:29 Mayank Porwal

    You can remove your for loop and do this instead:

    data = df.groupby(['ta', 'tb', 'tc'])['income'].sum()
    

    Then plot it.

  • answered 2018-11-08 07:34 jezrael

    Use df.groupby(i) with plt.figure() for 3 separately pie graphs:

    a = ['ta', 'tb', 'tc']
    for i in a:
        d = df.groupby(i)['income'].sum()
        plt.figure()
        d.plot.pie(autopct='%.1f%%')