1

I want to create a dictionary where the key is a state name and the value is the sum of all the count corresponding to the state.

Example from dataFrame

State         Count
California    100
Colorado      100
California    54
Colorado      9254

I was trying to use the following, but not sure how to include a sum function inside this, also not sure if I should be using a zip method here:

df_dict = dict(zip(df.State, df.Count))
print df_dict

2 Answers 2

1

Calculate the sum by group in the dataframe and then select the 'Count' column and use to_dict() to convert the resulting Series to dictionary:

df_dict = df.groupby('State').agg('sum')['Count'].to_dict()
print df_dict

If you have more columns in the df than those two you probably want to do

df[['State','Count']].groupby('State').agg('sum')['Count'].to_dict()

to avoid calculating the sum for all of the columns.

Edit:

as EdChum suggested .agg('sum') can be shortened to .sum() which gives you the same result.

Sign up to request clarification or add additional context in comments.

4 Comments

perfect, not sure if the ['Count'] is needed. unless i am missing something? @Pekka
It is needed to select the first (in this case only) column from the resulting DataFrame. Try it without it. You will get a nested dictionary. In that case your desired result is in the Count key.
So when we use the ['Count'] selection we get Series instead of DataFrame. Then we convert the Series (not DataFrame) to dictionary. Note that also DataFrames can be converted to dictionary with to_dict() but the result is different (nested dict). See: pandas.pydata.org/pandas-docs/stable/generated/… and pandas.pydata.org/pandas-docs/stable/generated/…
I think the agg is unnecessary here
1

That won't work properly, since the dict constructor will replace the value of each state as it goes, rather than summing. It's not a one-liner, but:

from collections import Counter

df_dict = Counter()
for state, count in zip(df.State, df.Count):
    df_dict[state] += count

will get you the total counts, and the Counter class has some extra functionality that may be useful for dictionaries of counts.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.