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

Kum*_* AK 2 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']})
Run Code Online (Sandbox Code Playgroud)

DF

我需要得到国家明智的独特产品和下面这样的用户

DF2

谢谢

raf*_*elc 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
Run Code Online (Sandbox Code Playgroud)

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

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)
Run Code Online (Sandbox Code Playgroud)

并且使用.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
Run Code Online (Sandbox Code Playgroud)

说明哪些用户/产品,

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
Run Code Online (Sandbox Code Playgroud)

  • @ user3483203他刚刚编辑它来添加它哈哈.Kumar,很简单,我会在1分钟内编辑 (2认同)