AttributeError:' SeriesGroupby'对象没有属性' tolist'

发布于 2025-02-13 03:05:20 字数 866 浏览 1 评论 0原文

在熊猫的数据框架中:我要计算stroke coulmn中有多少个值1,对于Residence_type列中的每个值。为了计算多少1,我认为stroke列将其转换为列表,我认为更容易。

因此,例如,中的值 Residence_typestroke> Stroke column中具有300次1。 。

数据是这样的:

    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

代码:

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

最终目标是在 1 出现的次数之间找到差异,对于中的每个值Residence_Type 列(农村或城市)。

我在做对吗?这是什么错误?

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:

    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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゃ懵逼小萝莉 2025-02-20 03:05:20

不确定我需要做什么。请尝试过滤器proke == 1,groupby and count;

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

          

                   Stroke_Count
Residence_type              
Rural                      3
Urban                      2

如果您需要类别之间的差异,可以尝试以下操作

 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

Not sure I got what you need done. Please try filter stroke==1, groupby and 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
提笔书几行 2025-02-20 03:05:20

假设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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文