简体   繁体   English

AttributeError:“SeriesGroupBy”对象没有属性“tolist”

[英]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.在 Panda 的数据框中:我想计算Residence_type列中的每个值在stroke列中有多少个值1 In order to count how much 1 there is, I convert the stroke column to a list, easier I think.为了计算有多少1 ,我将stroke列转换为列表,我认为更容易。

So for example, the value Rural in Residence_type has 300 times 1 in the stroke column.. and so on.例如, Residence_type中的Rural值在stroke列中有 300 乘以1 .. 以此类推。

The data is something like this:数据是这样的:

    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).最终目标是找出Residence_type列(农村或城市)中每个值出现值1的次数之间的差异

Am I doing it right?我做对了吗? what is this error ?这是什么错误?

Not sure I got what you need done.不知道我得到了你需要做的事情。 Please try filter stroke==1, groupby and count;请尝试 filter stroke==1, groupby 和 count;

df.query("Stroke==1").groupby('Residence_type')['Stroke'].agg('count').to_frame('Stroke_Count')

          

                   Stroke_Count
Residence_type              
Rural                      3
Urban                      2

You could try the following if you need the differences between categories如果您需要类别之间的差异,您可以尝试以下操作

 df1 =df.query("Stroke==1").groupby('Residence_type')['Stroke'].agg('count').to_frame('Stroke_Count')
df1.loc['Diff'] = abs(df1.loc['Rural']-df1.loc['Urban'])
print(df1)
    

                    Stroke_Count
Residence_type              
Rural                      3
Urban                      2
Diff                       1

Assuming that Stroke only contains 1 or 0, you can do:假设Stroke仅包含 1 或 0,您可以执行以下操作:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么熊猫给出AttributeError:'SeriesGroupBy'对象没有属性'pct'? - Why Pandas gives AttributeError: 'SeriesGroupBy' object has no attribute 'pct'? Bokeh:AttributeError:'DataFrame'对象没有属性'tolist' - Bokeh: AttributeError: 'DataFrame' object has no attribute 'tolist' AttributeError:“ DataFrame”对象没有属性“ tolist” - AttributeError: 'DataFrame' object has no attribute 'tolist' AttributeError: 'NDArray' 对象没有属性 'ravel' 或 'tolist' - AttributeError: 'NDArray' object has no attribute 'ravel' or 'tolist' 根据日期条件创建列,但出现此错误 AttributeError: 'SeriesGroupBy' object has no attribute 'sub'? - Create column based on date conditions, but I get this error AttributeError: 'SeriesGroupBy' object has no attribute 'sub'? 获取 AttributeError: 'builtin_function_or_method' object 没有属性 'tolist' - Getting AttributeError: 'builtin_function_or_method' object has no attribute 'tolist' 'SeriesGroupBy' 对象没有属性 '_aggregate_item_by_item' - 'SeriesGroupBy' object has no attribute '_aggregate_item_by_item' 获取“SeriesGroupBy”object 在加密数据的 pandas 重采样中没有属性“high” - getting 'SeriesGroupBy' object has no attribute 'high' in pandas resample of crypto data AttributeError: 'AttributeError' object 没有属性 'To' - AttributeError: 'AttributeError' object has no attribute 'To' AttributeError: '对象没有属性' - AttributeError: 'Object has no attribute'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM