pandas 如何从嵌套字典创建嵌套数据框架?

lawou6xi  于 2023-08-01  发布在  其他
关注(0)|答案(3)|浏览(104)

我有下面的字典:

nested_dict = {
    'Type A':   {'Type A':  10, 'Type B': 20},
    'Type EE':  {'Type B':  40, 'Type C': 50, 'Type A': 60},
    'Type FFF': {'Type ZZ': 70, 'Type FFF': 80, 'Type A': 90, 'Type AA': 1}
}

字符串
是否可以从这个字典中创建以下pandas dataframe:

|--------------------------------|
|    class  |      predictions   |
|           |--------------------|
|           |  TYPE    | COUTER  |
|--------------------------------|
|           | Type A   |  10     | 
| Type A    | Type B   |  20     |
|--------------------------------|
|           | Type B   |  40     |
| Type EE   | Type C   |  50     |
|           | Type A   |  60     |
|--------------------------------|
|           | Type zz  |  70     |
| Type FFF  | Type FFF |  80     |
|           | Type A   |  90     |
|           | Type AA  |  1      |
|--------------------------------|

t40tm48m

t40tm48m1#

可能的解决方案:

(pd.DataFrame(nested_dict).stack().swaplevel().reset_index()
 .set_axis(['Class', 'Type', 'Counter'], axis=1)
 .sort_values(['Class', 'Type']))

字符串
输出量:

Class      Type  Counter
0    Type A    Type A     10.0
3    Type A    Type B     20.0
1   Type EE    Type A     60.0
4   Type EE    Type B     40.0
5   Type EE    Type C     50.0
2  Type FFF    Type A     90.0
8  Type FFF   Type AA      1.0
7  Type FFF  Type FFF     80.0
6  Type FFF   Type ZZ     70.0

c86crjj0

c86crjj02#

import pandas as pd

nested_list=[]
for cl, predictions in nested_dict.items():
    for type_cat, counter in predictions.items():
        nested_list.append((cl, type_cat, counter))

df = pd.DataFrame(nested_list, columns=['CLASS', 'TYPE', 'COUNTER'])

df=df.sort_values(by='CLASS')

print(df)

字符串
输出:

CLASS      TYPE  COUNTER
0    Type A    Type A       10
1    Type A    Type B       20
2   Type EE    Type B       40
3   Type EE    Type C       50
4   Type EE    Type A       60
5  Type FFF   Type ZZ       70
6  Type FFF  Type FFF       80
7  Type FFF    Type A       90
8  Type FFF   Type AA        1

7eumitmz

7eumitmz3#

import pandas as pd
from tabulate import tabulate

# Given nested dictionary
nested_dict = {
    'Type A':   {'Type A':  10, 'Type B': 20},
    'Type EE':  {'Type B':  40, 'Type C': 50, 'Type A': 60},
    'Type FFF': {'Type ZZ': 70, 'Type FFF': 80, 'Type A': 90, 'Type AA': 1}
}

# Convert the nested dictionary into a flat list of tuples
data = []
for key, values in nested_dict.items():
    for sub_key, sub_value in values.items():
        data.append((key, sub_key, sub_value))

# Create the pandas DataFrame from the flat list of tuples
df = pd.DataFrame(data, columns=['class', 'TYPE', 'COUTER'])

# If you want to sort the DataFrame by the 'class' column
df = df.sort_values(by='class').reset_index(drop=True)

# Convert DataFrame to tabulate format and print with vertical lines using the 'pipe' table format
table = tabulate(df, headers='keys', tablefmt='pipe',
                 showindex=False, numalign="center", stralign="center")

# Print the tabulated DataFrame
print(table)

字符串
这是输出?

|  class   |   TYPE   |  COUTER  |
|:--------:|:--------:|:--------:|
|  Type A  |  Type A  |    10    |
|  Type A  |  Type B  |    20    |
| Type EE  |  Type B  |    40    |
| Type EE  |  Type C  |    50    |
| Type EE  |  Type A  |    60    |
| Type FFF | Type ZZ  |    70    |
| Type FFF | Type FFF |    80    |
| Type FFF |  Type A  |    90    |
| Type FFF | Type AA  |    1     |

相关问题