本文介绍了把收款柜台变成字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从函数中得到了一个收集结果:
I have a collection outcome resulting from the function:
Counter(df.email_address)
它返回每个电子邮件地址及其重复计数.
it returns each individual email address with the count of its repetitions.
Counter({nan: 1618, 'store@kiddicare.com': 265, 'testorders@worldstores.co.uk': 1})
我想做的就是像使用字典一样使用它,并在其中创建一个熊猫数据框,其中两列用于电子邮件地址,一列用于关联的值.
what I want to do is to use it as if it was a dictionary and create a pandas dataframe out of it with two columns one for email addresses and one for the value associated.
我尝试过:
dfr = repeaters.from_dict(repeaters, orient='index')
但是我遇到了以下错误:
but i got the following error:
AttributeError: 'Counter' object has no attribute 'from_dict'
看起来Counter不是字典,这使事情变得如此.关于如何将其附加到df的任何想法?
It makes thing that Counter is not a dictionary as it looks like. Any idea on how to append it to a df?
推荐答案
d = {}
cnt = Counter(df.email_address)
for key, value in cnt.items():
d[key] = value
编辑
或者,@ Trif Nefzger的建议是:
Or, how @Trif Nefzger suggested:
d = dict(Counter(df.email_address))
这篇关于把收款柜台变成字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!