প্রথমে, আসুন একটি নেস্টেড অভিধান −
তৈরি করি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
মাল্টি-ইনডেক্স ডেটাফ্রেমে রূপান্তর করুন -
pd.DataFrame(new_dict)
উদাহরণ
নিম্নলিখিত কোড -
import pandas as pd
# Create Nested dictionary
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
# converting to multiindex 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