Created
June 4, 2021 18:56
-
-
Save bruno-uy/66e539c2b119b31f8e4ec4d09118af89 to your computer and use it in GitHub Desktop.
Multiindex pandas DataFrame to multi level nested dictionary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
df = pd.DataFrame({"A": [0, 1, 0], "B": ["a", "b", "c"], "C": [1, 2, 3]}) | |
df.set_index(["A", "C"], drop=False, inplace=True) | |
result = {level: df.xs(level).to_dict("index") for level in df.index.levels[0]} | |
print(result[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment