如何获得唯一计数并动态生成文本

时间:2018-08-09 17:09:25

标签: python python-3.x pandas

我有一个这样的数据框

df = pd.DataFrame({
    'User':['101','101','102','102','102','101','102','103','103','103','101'],
    'Product':['x','xy','y','z','z','x','y','z','x','y',''],
    'Country':['India','India','India','Brazil','India','UK','UK','Brazil','India','UK','USA']})

df

并且我需要在以下国家/地区获得独特的产品和用户

df2

谢谢

1 个答案:

答案 0 :(得分:7)

IIUC,使用nuniqueagg

df.groupby('Country').nunique()[['User', 'Product']].agg(lambda f: "{} users and {} products".format(f['User'], f['Product']), 1)

Country
Brazil    2 users and 1 products
India     3 users and 4 products
UK        3 users and 2 products
dtype: object

如果要自定义您的代表,可以构建更详细的功能,例如:

def repr_(f):
    users = "{} user(s)".format(f['User']) if f['User'] else ''
    products = "{} product(s)".format(f['Product']) if f['Product'] else ''
    z = [str_ for str_ in (users, products) if str_]
    return " and ".join(z)

并使用.agg(repr_, 1),如果只有一个用户,只有一个产品或两者兼有,则可以使用。

Country
Brazil    2 user(s) and 1 product(s)
India     3 user(s) and 4 product(s)
UK        3 user(s) and 2 product(s)
USA                        1 user(s)
dtype: object

要说明哪些用户/产品,

def repr_(s):
    u, p = s['User'], s['Product']
    us, pr = ("{} user(s) ({})".format(len(u), ', '.join(u)) if len(u) else '',\
             "{} product(s) ({})".format(len(p), ', '.join(p)) if len(p) else '')
    z = [str_ for str_ in [us, pr] if str_]
    return " and ".join(z)

df.groupby('Country').agg(lambda s: set([x for x in s if x])).agg(repr_,1)

Country
Brazil            2 user(s) (102, 103) and 1 product(s) (z)
India     3 user(s) (101, 102, 103) and 4 product(s) (z,...
UK        3 user(s) (101, 102, 103) and 2 product(s) (y, x)
USA                                         1 user(s) (101)
dtype: object