Pandas分组组中的唯一值 [英] Unique values within Pandas group of groups

查看:87
本文介绍了Pandas分组组中的唯一值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框需要分组,然后再分组.从子组中,我需要返回子组是什么以及列的唯一值.

I have a dataframe that I need to group, then subgroup. From the subgroups I need to return what the subgroup is as well as the unique values for a column.

df = pandas.DataFrame({'country': pandas.Series(['US', 'Canada', 'US', 'US']),
                       'gender': pandas.Series(['male', 'female', 'male', 'female']),
                       'industry': pandas.Series(['real estate', 'shipping', 'telecom', 'real estate']),
                       'income': pandas.Series([1, 2, 3, 4])})

def subgroup(g):
    return g.groupby(['gender'])

s = df.groupby(['country']).apply(subgroup)

从s中,如何计算行业"的唯一性以及将其归为哪个性别"?

From s, how can I compute the uniques of "industry" as well as which "gender" it's grouped for?

--------------------------------------------
| US     | male   | [real estate, telecom] |
|        |----------------------------------
|        | female | [real estate]          |
--------------------------------------------
| Canada | female | [shipping]             |
--------------------------------------------

推荐答案

您无需定义该函数,只需使用groupby()和unique()即可解决您的问题;

you dont need to define that function, you can solve your problem with groupby() and unique() solely;

尝试:

df.groupby(['country','gender'])['industry'].unique()

输出:

country  gender
Canada   female                [shipping]
US       female             [real estate]
         male      [real estate, telecom]
Name: industry, dtype: object

希望有帮助!

这篇关于Pandas分组组中的唯一值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆