我有一个这样的数据框

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']})

python - 如何获得唯一计数并动态生成文本-LMLPHP
我需要得到国家的独特产品和用户如下
python - 如何获得唯一计数并动态生成文本-LMLPHP
谢谢

最佳答案

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

如果要自定义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)

使用.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

关于python - 如何获得唯一计数并动态生成文本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51772556/

10-12 22:00