প্রথমে একটি নেস্টেড ডিকশনারি তৈরি করুন:
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
Multi-index 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
# Multi-index DataFrame-এ কনভার্ট
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