I am looking for a way to create a nested named tuple from a pandas dataframe.
The object d is the expected output. I am not sure if the aggregation must be done directly in Pandas then the conversion to NamedTuple should be done afterward?
from typing import NamedTuple
from typing import List
import pandas as pd
if __name__ == "__main__":
data = [["tom", 10, "ab 11"], ["nick", 15, "ab 22"], ["juli", 14, "ab 11"]]
People = pd.DataFrame(data, columns=["Name", "Age", "PostalCode"])
names = list(People[["Name"]].itertuples(name="Names", index=False))
postal_codes = list(
People[["PostalCode"]].itertuples(name="PostalCode", index=False)
)
# ...
# ... The code after produce the expected output even if the name of the NamedTuple doesn't matter
PeopleName = NamedTuple("PeopleName", [("Name", str)])
PeoplePC = NamedTuple("PeoplePC", [("PostalCode", str)])
Demography = NamedTuple(
"Demography", [("names", List[PeopleName]), ("postalcodes", PeoplePC)]
)
d = [
Demography(
[PeopleName(Name="tom"), PeopleName(Name="juli")],
PeoplePC(PostalCode="ab 11"),
),
Demography([PeopleName(Name="nick")], PeoplePC(PostalCode="ab 22"),),
]