Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

AttributeError: 'SeriesGroupBy' object has no attribute 'tolist'

In a Panda’s dataframe: I want to count how many of value 1 there is, in the stroke coulmn, for each value in the Residence_type column. In order to count how much 1 there is, I convert the stroke column to a list, easier I think.

So for example, the value Rural in Residence_type has 300 times 1 in the stroke column.. and so on.

The data is something like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    Residence_type  Stroke
0       Rural         1
1       Urban         1
2       Urban         0
3       Rural         1
4       Rural         0
5       Urban         0
6       Urban         0 
7       Urban         1
8       Rural         0
9       Rural         1

The code:

grpby_variable = data.groupby('stroke')
grpby_variable['Residence_type'].tolist().count(1)

the final goal is to find the difference between the number of times the value 1 appears, for each value in the Residence_type column (rural or urban).

Am I doing it right? what is this error ?

>Solution :

Assuming that Stroke only contains 1 or 0, you can do:

result_df = df.groupby('Residence_type').sum()

>>> result_df
                Stroke
Residence_type        
Rural                3
Urban                2

>>> result_df.Stroke['Rural'] - result_df.Stroke['Urban']
1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading