# Pandas DataFrame to nested dict
```python
# 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}}}}}}}}}}
```
Raw data
{
"_id": null,
"home_page": "https://github.com/hansalemaos/a_pandas_ex_to_dict",
"name": "a-pandas-ex-to-dict",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "pandas,crosstab,DataFrame,merge,concat,Series",
"author": "Johannes Fischer",
"author_email": "<aulasparticularesdealemaosp@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/5a/87/281275b8824ec035c95eeaabac7031ebefbeca7a4bdb95d5d01e49ae91ea/a_pandas_ex_to_dict-0.10.tar.gz",
"platform": null,
"description": "\n# Pandas DataFrame to nested dict\n\n\n\n```python\n\n\n\n# from: https://stackoverflow.com/a/74810722/15096247\n\n\n\n$pip install a-pandas-ex-to-dict\n\n\n\nfrom a_pandas_ex_to_dict import pd_add_df_to_nested_dict\n\nimport pandas as pd\n\nfrom pprint import pprint\n\n\n\npd_add_df_to_nested_dict()\n\n\n\ndf = pd.read_csv(\"https://github.com/pandas-dev/pandas/raw/main/doc/data/titanic.csv\")\n\ndf = df[:10]\n\ndfdict2 = df.d_to_nested_dict([\"Survived\", \"Pclass\", \"Embarked\", \"Fare\", \"Cabin\"])\n\ndfdict = df.d_to_nested_dict()\n\npprint(dfdict2)\n\npprint(dfdict)\n\n\n\n\n\n{0: {1: {'S': {51.8625: {'E46': 1}}},\n\n 3: {'Q': {8.4583: {}}, 'S': {7.25: {}, 8.05: {}, 21.075: {}}}},\n\n 1: {1: {'C': {71.2833: {'C85': 1}}, 'S': {53.1: {'C123': 1}}},\n\n 2: {'C': {30.0708: {}}},\n\n 3: {'S': {7.925: {}, 11.1333: {}}}}}\n\n{1: {0: {3: {'Braund, Mr. Owen Harris': {'male': {22.0: {1: {0: {'A/5 21171': {7.25: 1}}}}}}}}},\n\n 2: {1: {1: {'Cumings, Mrs. John Bradley (Florence Briggs Thayer)': {'female': {38.0: {1: {0: {'PC 17599': {71.2833: 1}}}}}}}}},\n\n 3: {1: {3: {'Heikkinen, Miss. Laina': {'female': {26.0: {0: {0: {'STON/O2. 3101282': {7.925: 1}}}}}}}}},\n\n 4: {1: {1: {'Futrelle, Mrs. Jacques Heath (Lily May Peel)': {'female': {35.0: {1: {0: {'113803': {53.1: 1}}}}}}}}},\n\n 5: {0: {3: {'Allen, Mr. William Henry': {'male': {35.0: {0: {0: {'373450': {8.05: 1}}}}}}}}},\n\n 6: {0: {3: {'Moran, Mr. James': {'male': {}}}}},\n\n 7: {0: {1: {'McCarthy, Mr. Timothy J': {'male': {54.0: {0: {0: {'17463': {51.8625: 1}}}}}}}}},\n\n 8: {0: {3: {'Palsson, Master. Gosta Leonard': {'male': {2.0: {3: {1: {'349909': {21.075: 1}}}}}}}}},\n\n 9: {1: {3: {'Johnson, Mrs. Oscar W (Elisabeth Vilhelmina Berg)': {'female': {27.0: {0: {2: {'347742': {11.1333: 1}}}}}}}}},\n\n 10: {1: {2: {'Nasser, Mrs. Nicholas (Adele Achem)': {'female': {14.0: {1: {0: {'237736': {30.0708: 1}}}}}}}}}}\n\n```\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Pandas DataFrame to nested dict",
"version": "0.10",
"split_keywords": [
"pandas",
"crosstab",
"dataframe",
"merge",
"concat",
"series"
],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "d4e70f483093a8e2f2363cf704ec3ab9",
"sha256": "6c472b44fca645b5b2bfb417031e9b297b319ccb3f661c12551d49dfbbb80257"
},
"downloads": -1,
"filename": "a_pandas_ex_to_dict-0.10-py3-none-any.whl",
"has_sig": false,
"md5_digest": "d4e70f483093a8e2f2363cf704ec3ab9",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 5812,
"upload_time": "2022-12-16T01:15:03",
"upload_time_iso_8601": "2022-12-16T01:15:03.030329Z",
"url": "https://files.pythonhosted.org/packages/3f/16/15235a3959523bb9430a62d0eb71742baebd3014c600e2b312af1030eeaa/a_pandas_ex_to_dict-0.10-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"md5": "beb9272885c80e95706ffee546a479e7",
"sha256": "c24720374fa01295f2178a6fc9f51a75c9cb19a868436ecddaed70cf888640f6"
},
"downloads": -1,
"filename": "a_pandas_ex_to_dict-0.10.tar.gz",
"has_sig": false,
"md5_digest": "beb9272885c80e95706ffee546a479e7",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 3948,
"upload_time": "2022-12-16T01:15:04",
"upload_time_iso_8601": "2022-12-16T01:15:04.375290Z",
"url": "https://files.pythonhosted.org/packages/5a/87/281275b8824ec035c95eeaabac7031ebefbeca7a4bdb95d5d01e49ae91ea/a_pandas_ex_to_dict-0.10.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2022-12-16 01:15:04",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "hansalemaos",
"github_project": "a_pandas_ex_to_dict",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [],
"lcname": "a-pandas-ex-to-dict"
}