सबसे पहले, एक नेस्टेड डिक्शनरी बनाएं:
dictNested = {'Cricket': {'Boards': ['BCCI', 'CA', 'ECB'], 'Country': ['India', 'Australia', 'England']}, 'Football': {'Boards': ['TFA', 'TCSA', 'GFA'], 'Country': ['England', 'Canada', 'Germany']}}
अब, एक खाली डिक्शनरी बनाएं:
new_dict = {}
अब, मान असाइन करने के लिए लूप:
for outerKey, innerDict in dictNested.items():
for innerKey, values in innerDict.items():
new_dict[(outerKey, innerKey)] = values
मल्टी-इंडेक्स DataFrame में बदलें:
pd.DataFrame(new_dict)
उदाहरण
निम्न कोड है:
import pandas as pd
# नेस्टेड डिक्शनरी बनाएं
dictNested = {'Cricket': {'Boards': ['BCCI', 'CA', 'ECB'], 'Country': ['India', 'Australia', 'England']}, 'Football': {'Boards': ['TFA', 'TCSA', 'GFA'], 'Country': ['England', 'Canada', 'Germany']}}
print("\nNested Dictionary...\n", dictNested)
new_dict = {}
for outerKey, innerDict in dictNested.items():
for innerKey, values in innerDict.items():
new_dict[(outerKey, innerKey)] = values
# मल्टीइंडेक्स डेटाफ्रेम में बदलें
print("\nMulti-index DataFrame...\n", pd.DataFrame(new_dict))
आउटपुट
यह निम्न आउटपुट उत्पन्न करेगा:
Nested Dictionary...
{'Cricket': {'Country': ['India', 'Australia', 'England'], 'Boards': ['BCCI', 'CA', 'ECB']}, 'Football': {'Country': ['England', 'Canada', 'Germany'], 'Boards': ['TFA', 'TCSA', 'GFA']}}
Multi-index DataFrame...
Cricket Football
Boards Country Boards Country
0 BCCI India TFA England
1 CA Australia TCSA Canada
2 ECB England GFA Germany