gpt4 book ai didi

python - Pandas :如果列中的值是字典,则使用 groupby

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

我有数据框

category  dictionary
moto {'motocycle':10, 'buy":8, 'motocompetition':7}
shopping {'buy':200, 'order':20, 'sale':30}
IT {'iphone':214, 'phone':1053, 'computer':809}
shopping {'zara':23, 'sale':18, 'sell':20}
IT {'lenovo':200, 'iphone':300, 'mac':200}

我需要按类别分组,结果连接字典并选择 3 个具有最大值的键。接下来获取数据框,在 category 列中我有唯一的类别,在 data 列中我有带键的列表。

我知道,我可以使用 Counter 来连接字典,但我不知道,如何对类别进行连接。欲望输出

category   data
moto ['motocycle', 'buy', 'motocompetition']
shopping ['buy', 'sale', 'zara']
IT ['phone', 'computer', 'iphone']

最佳答案

您可以使用 groupby带有自定义函数 nlargestIndex.tolist :

df = pd.DataFrame({
'category':['moto','shopping','IT','shopping','IT'],
'dictionary':
[{'motocycle':10, 'buy':8, 'motocompetition':7},
{'buy':200, 'order':20, 'sale':30},
{'iphone':214, 'phone':1053, 'computer':809},
{'zara':23, 'sale':18, 'sell':20},
{'lenovo':200, 'iphone':300, 'mac':200}]})

print (df)
category dictionary
0 moto {'motocycle': 10, 'buy': 8, 'motocompetition': 7}
1 shopping {'sale': 30, 'buy': 200, 'order': 20}
2 IT {'phone': 1053, 'computer': 809, 'iphone': 214}
3 shopping {'sell': 20, 'zara': 23, 'sale': 18}
4 IT {'lenovo': 200, 'mac': 200, 'iphone': 300}


import collections
import functools
import operator

def f(x):
#some possible solution for sum values of dict
#http://stackoverflow.com/a/3491086/2901002
return pd.Series(functools.reduce(operator.add, map(collections.Counter, x)))
.nlargest(3).index.tolist()

print (df.groupby('category')['dictionary'].apply(f).reset_index())
category dictionary
0 IT [phone, computer, iphone]
1 moto [motocycle, buy, motocompetition]
2 shopping [buy, sale, zara]

关于python - Pandas :如果列中的值是字典,则使用 groupby,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39994110/

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