Skip to main content

Pandas DataFrame to nested dict

Project description

Pandas DataFrame to nested dict

# from: https://stackoverflow.com/a/74810722/15096247



$pip install a-pandas-ex-to-dict



from a_pandas_ex_to_dict import pd_add_df_to_nested_dict

import pandas as pd

from pprint import pprint



pd_add_df_to_nested_dict()



df = pd.read_csv("https://github.com/pandas-dev/pandas/raw/main/doc/data/titanic.csv")

df = df[:10]

dfdict2 = df.d_to_nested_dict(["Survived", "Pclass", "Embarked", "Fare", "Cabin"])

dfdict = df.d_to_nested_dict()

pprint(dfdict2)

pprint(dfdict)





{0: {1: {'S': {51.8625: {'E46': 1}}},

     3: {'Q': {8.4583: {}}, 'S': {7.25: {}, 8.05: {}, 21.075: {}}}},

 1: {1: {'C': {71.2833: {'C85': 1}}, 'S': {53.1: {'C123': 1}}},

     2: {'C': {30.0708: {}}},

     3: {'S': {7.925: {}, 11.1333: {}}}}}

{1: {0: {3: {'Braund, Mr. Owen Harris': {'male': {22.0: {1: {0: {'A/5 21171': {7.25: 1}}}}}}}}},

 2: {1: {1: {'Cumings, Mrs. John Bradley (Florence Briggs Thayer)': {'female': {38.0: {1: {0: {'PC 17599': {71.2833: 1}}}}}}}}},

 3: {1: {3: {'Heikkinen, Miss. Laina': {'female': {26.0: {0: {0: {'STON/O2. 3101282': {7.925: 1}}}}}}}}},

 4: {1: {1: {'Futrelle, Mrs. Jacques Heath (Lily May Peel)': {'female': {35.0: {1: {0: {'113803': {53.1: 1}}}}}}}}},

 5: {0: {3: {'Allen, Mr. William Henry': {'male': {35.0: {0: {0: {'373450': {8.05: 1}}}}}}}}},

 6: {0: {3: {'Moran, Mr. James': {'male': {}}}}},

 7: {0: {1: {'McCarthy, Mr. Timothy J': {'male': {54.0: {0: {0: {'17463': {51.8625: 1}}}}}}}}},

 8: {0: {3: {'Palsson, Master. Gosta Leonard': {'male': {2.0: {3: {1: {'349909': {21.075: 1}}}}}}}}},

 9: {1: {3: {'Johnson, Mrs. Oscar W (Elisabeth Vilhelmina Berg)': {'female': {27.0: {0: {2: {'347742': {11.1333: 1}}}}}}}}},

 10: {1: {2: {'Nasser, Mrs. Nicholas (Adele Achem)': {'female': {14.0: {1: {0: {'237736': {30.0708: 1}}}}}}}}}}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

a_pandas_ex_to_dict-0.10.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

a_pandas_ex_to_dict-0.10-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file a_pandas_ex_to_dict-0.10.tar.gz.

File metadata

  • Download URL: a_pandas_ex_to_dict-0.10.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for a_pandas_ex_to_dict-0.10.tar.gz
Algorithm Hash digest
SHA256 c24720374fa01295f2178a6fc9f51a75c9cb19a868436ecddaed70cf888640f6
MD5 beb9272885c80e95706ffee546a479e7
BLAKE2b-256 5a87281275b8824ec035c95eeaabac7031ebefbeca7a4bdb95d5d01e49ae91ea

See more details on using hashes here.

File details

Details for the file a_pandas_ex_to_dict-0.10-py3-none-any.whl.

File metadata

File hashes

Hashes for a_pandas_ex_to_dict-0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 6c472b44fca645b5b2bfb417031e9b297b319ccb3f661c12551d49dfbbb80257
MD5 d4e70f483093a8e2f2363cf704ec3ab9
BLAKE2b-256 3f1615235a3959523bb9430a62d0eb71742baebd3014c600e2b312af1030eeaa

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page