Почему преобразование вложенного словаря Python в кадр данных pandas приводит к ошибке «не имеет атрибута 'items'»?Python

Программы на Python
Anonymous
Почему преобразование вложенного словаря Python в кадр данных pandas приводит к ошибке «не имеет атрибута 'items'»?

Сообщение Anonymous »

У меня есть вложенный словарь, хранящийся в переменной neged_dict_variable.
Словарь извлекается с помощью свойства SPSS valueLabels (Python)
type(nested_dict_variable) приводит к dict.
print(nested_dict_variable) приводит к результату {0: {1.0: '1 - низкий', 2.0: '2', 3.0: '3', 4.0: '4', 5.0: '5 - высокий', 99.0: "99 - не 'не знаю"}, 1: {0.0: '0 - нет', 1.0: '1 - да'}, 2: {1.0: '1 - А', 2.0: '2 - Б', 3.0: '3 - C'}}
Я пытаюсь преобразовать этот вложенный словарь в DataFrame pandas, но получаю следующую ошибку. Я не понимаю, почему возникает эта ошибка атрибута, учитывая, чтоnested_dict_variable является (или кажется) словарем!?
AttributeError Traceback (most recent call last)
File c:\mypythonfile.py:38
36 data_list = []
37 for outer_key, inner_dict in nested_dict_variable.items():
---> 38 for inner_key, value in inner_dict.items():
39 data_list.append({'Outer Key': outer_key, 'Inner Key': inner_key, 'Value': value})
41 df = pd.DataFrame(data_list)

AttributeError: 'ValueLabel' object has no attribute 'items'

Вот мой код:
# see: https://www.ibm.com/docs/en/spss-statis ... #d10392e74
import spss
# import pandas
import pandas as pd

# read spss-data
file = r"C:\SPSS-SampleData1.sav"
spss.Submit(
f"""
GET FILE='{file}'.
"""
)

var_index = []
nested_dict_variable= {}

# initialise the handling of spss commands
spss.StartDataStep()

# access active dataset (the one that was read above)
datasetObj = spss.Dataset()

# get a list of variable objects
varListObj = datasetObj.varlist
for var in varListObj:
var_index.append(var.index)
nested_dict_variable[var.index] = var.valueLabels

spss.EndDataStep()

##### CREATE DATAFRAMES #####

# convert nested dictionary to Pandas DataFrame
data_list = []
for outer_key, inner_dict in nested_dict_variable.items():
for inner_key, value in inner_dict.items():
data_list.append({'Outer Key': outer_key, 'Inner Key': inner_key, 'Value': value})

df = pd.DataFrame(data_list)

# end spss process
spss.StopSPSS()


Подробнее здесь: https://stackoverflow.com/questions/787 ... -result-in

Вернуться в «Python»