[英]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.