pandas 将嵌套字典转换为 mutiIndex 行和列
Posted
技术标签:
【中文标题】pandas 将嵌套字典转换为 mutiIndex 行和列【英文标题】:pandas convert a nested dictionary to mutiIndex rows and columns 【发布时间】:2020-06-11 21:20:27 【问题描述】:我有一个嵌套字典,我想将它变成一个多索引行和列,如下所示。但是我的数据以某种方式丢失在表中。
test= 12: 'Category 1': 'TestA': 'att_1': 1, 'att_2': 'whatever', 'TestB': 'att_1': 3, 'att_2': 'spring', 'Category 2': 'TestA': 'att_1': 23, 'att_2': 'another', 'TestB': 'att_1': 9, 'att_2': 'summer', 15: 'Category 1': 'TestA': 'att_1': 10, 'att_2': 'foo', 'TestB': 'att_1': 29, 'att_2': 'fall', 'Category 2': 'TestA': 'att_1': 30, 'att_2': 'bar', 'TestB': 'att_1': 36, 'att_2': 'winter'
columns=pd.MultiIndex.from_arrays([['TestA','TestA','TestB','TestB'],['att_1','att_2','att_1','att_2']])
我想要的格式:
TestA TestB
att_1 att_2 att_1 att_2
12 Category 1 NaN NaN NaN NaN
Category 2 NaN NaN NaN NaN
15 Category 1 NaN NaN NaN NaN
Category 2 NaN NaN NaN NaN
我做到了
pd.DataFrame(test,index=pd.MultiIndex.from_arrays([[12,12,15,15],['Category 1','Category 2','Category 1','Category 2']]),columns=pd.MultiIndex.from_arrays([['TestA','TestA','TestB','TestB'],['att_1','att_2','att_1','att_2']]))
我的数据丢失如下:
TestA TestB
att_1 att_2 att_1 att_2
12 Category 1 NaN NaN NaN NaN
Category 2 NaN NaN NaN NaN
15 Category 1 NaN NaN NaN NaN
Category 2 NaN NaN NaN NaN
如果我只有 multiIndex 行,它会工作,但我想要 multiIndex 行和列。
pd.DataFrame.from_dict((i,j): test[i][j]
for i in test.keys()
for j in test[i].keys(),
orient='index')
TestA TestB
12 Category 1 'att_1': 1, 'att_2': 'whatever' 'att_1': 3, 'att_2': 'spring'
Category 2 'att_1': 23, 'att_2': 'another' 'att_1': 9, 'att_2': 'summer'
15 Category 1 'att_1': 10, 'att_2': 'foo' 'att_1': 29, 'att_2': 'fall'
Category 2 'att_1': 30, 'att_2': 'bar' 'att_1': 36, 'att_2': 'winter
【问题讨论】:
advanced indexing的pandas信息你查了吗? 【参考方案1】:你可以得到所需的数据框为:
import pandas as pd
import numpy as np
test= 12: 'Category 1': 'TestA': 'att_1': 1, 'att_2': 'whatever', 'TestB': 'att_1': 3, 'att_2': 'spring', 'Category 2': 'TestA': 'att_1': 23, 'att_2': 'another', 'TestB': 'att_1': 9, 'att_2': 'summer', 15: 'Category 1': 'TestA': 'att_1': 10, 'att_2': 'foo', 'TestB': 'att_1': 29, 'att_2': 'fall', 'Category 2': 'TestA': 'att_1': 30, 'att_2': 'bar', 'TestB': 'att_1': 36, 'att_2': 'winter'
# Row indexes
row_index = [[12,12,15,15],['Category 1','Category 2','Category 1','Category 2']]
# Column indexes
col_index = [['TestA','TestA','TestB','TestB'],['att_1','att_2','att_1','att_2']]
# Values row wise
values =[1,'whatever',3,'spring',23,'another',9,'summer',10,'foo',29,'fall',30,'bar',36,'winter']
# Convert the list of values to numpy array
value = np.array(values)
# Reshape the value as (4,4) array as the matrix/dataframe is of shape (4,4)
value = value.reshape(4,4)
# Get your required data frame
pd.DataFrame(value, index=row_index, columns=col_index)
【讨论】:
以上是关于pandas 将嵌套字典转换为 mutiIndex 行和列的主要内容,如果未能解决你的问题,请参考以下文章