turning a collections counter into dictionary

Deadly 提交于 2020-01-16 04:30:27

问题


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.

I tried with:

dfr = repeaters.from_dict(repeaters, orient='index')

but i got the following error:

AttributeError: 'Counter' object has no attribute 'from_dict'

It makes thing that Counter is not a dictionary as it looks like. Any idea on how to append it to a df?


回答1:


d = {}
cnt = Counter(df.email_address)
for key, value in cnt.items():
    d[key] = value

EDIT

Or, how @Trif Nefzger suggested:

d = dict(Counter(df.email_address))



回答2:


as ajcr wrote at the comment, from_dict is a method that belongs to dataframe and thus you can write the following to achieve your goal:

from collections import Counter
import pandas as pd

repeaters = Counter({"nan": 1618, 'store@kiddicare.com': 265, 'testorders@worldstores.co.uk': 1})

dfr = pd.DataFrame.from_dict(repeaters, orient='index')
print dfr

Output:

testorders@worldstores.co.uk     1
nan                           1618
store@kiddicare.com            265



回答3:


Alternatively you could use pd.Series.value_counts, which returns a Series object.

df.email_address.value_counts(dropna=False)

Sample output:

b@y.com    2
a@x.com    1
NaN        1
dtype: int64

This is not exactly what you asked for but looks like what you'd like to achieve.



来源:https://stackoverflow.com/questions/31807945/turning-a-collections-counter-into-dictionary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!