gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 20:32:51 25 4
gpt4 key购买 nike

我有这样一个数据框

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

谢谢

最佳答案

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com