简体   繁体   English

python pandas:如何对列中的每个值进行分组并计算条件?

[英]python pandas: how to group by and count with a condition for every value in a column?

I have table like this: 我有这样的表:

d  group
1   a
2   b
3   a
4   c
5   f

and I like to iterate over values of d and count number of rows that have group=a . 我想遍历d的值并计算具有group = a的行数。

Here is what I am doing now, but It does not work: 这是我现在正在做的事情,但是它不起作用:

for index,row in df.iterrows():
    for x in (1,5):
        if row['d'] > x:
            row['tp'] = df.groupby('group').agg(lambda x:x.manual_type=='a')

Can anybody help? 有人可以帮忙吗?

try: 尝试:

df['group'].value_counts()['a']

in general, you should NEVER use for loops in pandas. 通常,您不应该在熊猫中使用循环。 it's inefficient and usually recreating some existing functionality in the package. 它效率低下,通常会在软件包中重新创建一些现有功能。

暂无
暂无

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

相关问题 Python 和 Pandas:如何计算具有特定条件的列 - Python and Pandas: How count a column with specific condition 如何在python pandas中的同一列上进行分组并将一些值的唯一数和计数数作为聚合? - How to do group by and take count of unique and count of some value as aggregate on same column in python pandas? python pandas:按几列分组并计算一列的值 - python pandas : group by several columns and count value for one column Python / Pandas - 如何按两列分组,并计算两行之间第三列的值 - Python/Pandas - How to group by two columns and count rows with value from third column between two numbers 计算一个列的值连续变化n次以上的次数,连同pandas中的变化、分组依据和条件 - Count how many times a value of a column changes for more than n consecutive times, together with the changes, with group by, and condition in pandas 如何使用python pandas对列进行分组并按条件计算值? - How to groupby a column and count the values on condition using python pandas? Python Pandas新的dataframe列有group by和condition - Python Pandas new dataframe column with group by and condition 使用Pandas数据框按列计算组值 - Group Value Count By Column with Pandas Dataframe 计算 Pandas dataframe 的每一列中满足条件的值的数量 - Count the number of values that satisfy a condition in every column of a Pandas dataframe python - 如何在python pandas中分组并取一列的计数除以数据框第二列的唯一计数? - How to do group by and take Count of one column divide by count of unique of second column of data frame in python pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM