1

I am trying to create a multi-level nested dictionary from a pandas dataframe - In the below example I want to retrieve for every postal code, the sum of salary for each sex and age combination. The output must be a dictionary as presented in the Expected output comment.

from typing import NamedTuple, Sequence, Tuple

import pandas as pd

data = [
    ["tom", 22, "ab 11", "M", 5555],
    ["Rob", 22, "ab 11", "M", 9999],
    ["nick", 33, "ab 22", "M", 3333],
    ["juli", 18, "ab 11", "F", 2222],
]
people = pd.DataFrame(data, columns=["Name", "Age", "PostalCode", "Sex", "Salary"])

d = (
    people.groupby(["PostalCode", "Sex", "Age"])["Salary"]
    .apply(sum)
    .to_dict()
)

print(d)

# Expected output
print({"ab 11": {("M", 22): 15554, ("F", 18): 2222}, "ab 22": {("M", 33): 3333}})

1 Answer 1

2

Just change your solution a little and use additional dict comprehension

df = (
    people.groupby(["PostalCode", "Sex", "Age"])["Salary"]
          .sum()
          .unstack(0)
    )

d =  {col: df[col].dropna().to_dict() for col in df}

print(d)

Out[40]:
{'ab 11': {('F', 18): 2222.0, ('M', 22): 15554.0},
 'ab 22': {('M', 33): 3333.0}}
Sign up to request clarification or add additional context in comments.

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.