在python中进行汇总时过滤

在python中进行汇总时过滤,python,r,pandas,dplyr,pandas-groupby,Python,R,Pandas,Dplyr,Pandas Groupby,我是一名R用户,目前正在学习python 通常,我使用dplyr对数据进行分组和汇总。例如 data1 %>% dplyr::group_by(city) %>% dplyr::summarize(unique_customers = n_distinct(user_id, na.rm = TRUE), converted_customers = n_distinct(user_id[type == "CONVERTED"], na.rm

我是一名R用户,目前正在学习python

通常,我使用dplyr对数据进行分组和汇总。例如

data1 %>%
   dplyr::group_by(city) %>%
   dplyr::summarize(unique_customers = n_distinct(user_id, na.rm = TRUE),
                 converted_customers = n_distinct(user_id[type == "CONVERTED"], na.rm = TRUE)) %>%
   data.frame()
我已经能够实现group_by和unique_客户,但是在python中设置condition type==“CONVERTED”时遇到了一些问题。我该怎么做

编辑: 我现在的python代码:

data1.fillna(method = "ffill").groupby("city").agg({"user_id": "nunique"})

apply(lambda x:(x=='CONVERTED'):::与您希望在RI am中实现的类似,使用以下代码获取每个城市的唯一客户计数:data1.fillna(method=“ffill”).groupby(“city”).agg({“user_id:”nunique“}),但我希望应用另一列“type”中的过滤器,该列应过滤为仅转换,类似于sql中的when语句。这就是我被困的地方@roganjoshI对pandas或纯python都没问题。我只找到了“pandas groupby”标签。希望这能起作用。我已经为你添加了熊猫标签嘿,谢谢你的回答@神秘,我想这只会给转换的顾客,对吗?我需要独特的客户和转换的客户。
data1[data1.type=='CONVERTED'].fillna(method = "ffill").groupby("city").agg({"user_id": "nunique"})