📜  嵌套字典到多索引数据框

📅  最后修改于: 2022-05-13 01:54:52.424000             🧑  作者: Mango

嵌套字典到多索引数据框

Pandas DataFrame是一个二维大小可变、潜在异构的表格数据结构,带有标记的轴(行和列)。数据框是一种二维数据结构,即数据在行和列中以表格方式对齐。 Pandas DataFrame 由三个主要组件组成,数据、行和列。多索引数据帧是一个具有多级索引或分层索引的Pandas 数据帧。

Pandas 需要多索引值作为元组,而不是嵌套字典。因此,首先,我们需要将嵌套索引值转换为元组。

示例#1:

Python3
# Import module
import pandas as pd
  
# Nested dictionary to convert it
# into multiindex dataframe
nested_dict = {'A': {'a': [1, 2, 3,
                           4, 5],
                     'b': [6, 7, 8,
                           9, 10]},
  
               'B': {'a': [11, 12, 13,
                           14, 15],
                     'b': [16, 17, 18,
                           19, 20]}}
  
reformed_dict = {}
for outerKey, innerDict in nested_dict.items():
    for innerKey, values in innerDict.items():
        reformed_dict[(outerKey,
                       innerKey)] = values
  
# Multiindex dataframe
reformed_dict


Python3
multiIndex_df = pd.DataFrame(reformed_dict)
multiIndex_df


Python3
# Import module
import pandas as pd
  
# Nested dictionary to convert it into multiindex dataframe
nested_dict = {'India': {'State': ['Maharashtra', 'West Bengal',
                                   'Uttar Pradesh', 'Bihar', 'Karnataka'],
                         'Capital': ['Mumbai', 'Kolkata', 'Lucknow',
                                     'Patna', 'Bengaluru']},
  
               'America': {'State': ['California', 'Florida', 'Georgia',
                                     'Massachusetts', 'New York'],
                           'Capital': ['Sacramento', 'Tallahassee', 'Atlanta',
                                       'Boston', 'Albany']}}
  
reformed_dict = {}
for outerKey, innerDict in nested_dict.items():
    for innerKey, values in innerDict.items():
        reformed_dict[(outerKey, innerKey)] = values
  
# Display multiindex dataframe
multiIndex_df = pd.DataFrame(reformed_dict)
multiIndex_df


输出:



请注意,在reformed_dict 中,索引值在元组中。现在要将reformed_dict转换为多索引数据帧,我们可以使用pd.DataFrame()方法。

蟒蛇3

multiIndex_df = pd.DataFrame(reformed_dict)
multiIndex_df

输出:

在输出中,我们可以看到列的分层索引/多索引。

示例#2:

蟒蛇3

# Import module
import pandas as pd
  
# Nested dictionary to convert it into multiindex dataframe
nested_dict = {'India': {'State': ['Maharashtra', 'West Bengal',
                                   'Uttar Pradesh', 'Bihar', 'Karnataka'],
                         'Capital': ['Mumbai', 'Kolkata', 'Lucknow',
                                     'Patna', 'Bengaluru']},
  
               'America': {'State': ['California', 'Florida', 'Georgia',
                                     'Massachusetts', 'New York'],
                           'Capital': ['Sacramento', 'Tallahassee', 'Atlanta',
                                       'Boston', 'Albany']}}
  
reformed_dict = {}
for outerKey, innerDict in nested_dict.items():
    for innerKey, values in innerDict.items():
        reformed_dict[(outerKey, innerKey)] = values
  
# Display multiindex dataframe
multiIndex_df = pd.DataFrame(reformed_dict)
multiIndex_df

输出: