有没有更好的方法可以在多个条件下使用map函数对大熊猫进行分组?

时间:2019-01-08 13:41:26

标签: python excel pandas dataframe

我正在翻译熊猫Data-type中的excel公式。我有两个数据帧df1和df2,我将需要在第一列数据帧df1中对值进行计数,然后填充数据帧df2,其中在df1中计数的值等于在df2中的值。如何在下面的解决方案中检查第二个条件?

resource "azurerm_function_app" "func-app-1" { name = "${var.func_app_1}" location = "${data.azurerm_resource_group.core-rg.location}" resource_group_name = "${data.azurerm_resource_group.core-rg.name}" app_service_plan_id = "${data.azurerm_app_service_plan.app-service-plan-1.id}" storage_connection_string = "${data.azurerm_storage_account.storage-account-1.primary_connection_string}" version = "~1" https_only = "true" enabled = "true" always_on = "true" use_32_bit_worker_process = "false" websockets_enabled = "true" client_affinity_enabled = "false" app_settings = "${var.app_settings}" }

variable "app_settings" {
    description = "A key-value pair of App Settings"
    default     = {
        "FUNCTIONS_EXTENSION_VERSION" = "~1",
        "KeyVaultURI" = “”,
        "WEBSITE_NODE_DEFAULT_VERSION" = "6.5.0"
    }
}

COUNTIFS(pos!$D:$D,$A3,pos!$N:$N,$E3)

df1

使用相关问题的答案

      id      member        seq
 0   48299      Koif          1
 1   48299      Iki           1
 2   48299      Juju          2
 3   48299      PNik          3 
 4   48865      Lok           1 
 5   48865      Mkoj          2
 6   48865      Kino          1
 7   64865      Boni          1
 8   64865      Afriya        2
 9   50774      Amah          2

第一个条件df2的计数有效,以添加第二个条件。我在下面尝试了几种解决方案:

soln1:

    group_id      group_name  seq    count
 0   48299         e_sys       1       
 1   50774         Y3N         2       
 2   64865         nana        1       
 3   48865         juzti       1       

soln2:

df2['count'] = df2['group_id'].map(df1.groupby('id')['id'].count())

但是我似乎没有得到groupby的正确计数

预期结果:

df2['count'] = df2['seq'].map(df1.groupby(['seq'])['id'].count())

1 个答案:

答案 0 :(得分:1)

我想您可以先mergegroupby然后再map

merge = pd.merge(df2,df1, left_on=['group_id', 'seq'], right_on=['id','seq']).groupby('id')['id'].count()
df2['count'] = df2['group_id'].map(merge)


  group_id  group_name  seq count
0   48299   e_sys        1  2
1   50774    Y3N         2  1
2   64865   nana         1  1
3   48865   juzti        1  2