Я предполагаю, что вы ищете:
import pandas as pd
restructure_dict = {
(level1_key, level2_key, level3_key): values
for level1_key, level2_dict in myDic.items()
for level2_key, level3_dict in level2_dict.items()
for level3_key, values in level3_dict.items()
}
df = pd.DataFrame(restructure_dict).T.reset_index()
df = df.rename(columns={'level_0': 'brand', 'level_1': 'model', 'level_2': 'year'})
print(df)
, и результат будет:
brand model year transmission engine drivetrain wheels_hubs seat_belts_air_bags brakes lights body_paint fuel_system electrical suspension miscellaneous steering accessories-interior accessories-exterior windows_windshield
0 Acura CL 2003 4 1 NHTSA: 13 NHTSA: 8 NHTSA: 6 NHTSA: 6 NHTSA: 5 NHTSA: 2 NHTSA: 2 NHTSA: 2 NHTSA: 2 NHTSA: 1 NHTSA: 1 NaN NaN NaN
1 Acura CL 2002 2 NHTSA: 8 NHTSA: 2 NaN NHTSA: 3 NHTSA: 7 NaN NHTSA: 1 NHTSA: 1 NHTSA: 4 NHTSA: 2 NHTSA: 1 NHTSA: 1 NHTSA: 3 NHTSA: 1 NHTSA: 1
Другое возможное решение может быть таким:
import pandas as pd
restructure_dict = {
(level1_key, level2_key, level3_key): values
for level1_key, level2_dict in myDic.items()
for level2_key, level3_dict in level2_dict.items()
for level3_key, values in level3_dict.items()
}
df = pd.DataFrame(restructure_dict)
print(df)
И вывод будет:
Acura
CL
2003 2002
transmission 4 2
engine 1 NHTSA: 8
drivetrain NHTSA: 13 NHTSA: 2
wheels_hubs NHTSA: 8 NaN
seat_belts_air_bags NHTSA: 6 NHTSA: 3
brakes NHTSA: 6 NHTSA: 7
lights NHTSA: 5 NaN
body_paint NHTSA: 2 NHTSA: 1
fuel_system NHTSA: 2 NHTSA: 1
electrical NHTSA: 2 NHTSA: 4
suspension NHTSA: 2 NHTSA: 2
miscellaneous NHTSA: 1 NHTSA: 1
steering NHTSA: 1 NHTSA: 1
accessories-interior NaN NHTSA: 3
accessories-exterior NaN NHTSA: 1
windows_windshield NaN NHTSA: 1
Другим вариантом будет транспонированная версия вышеуказанного результата:
import pandas as pd
restructure_dict = {
(level1_key, level2_key, level3_key): values
for level1_key, level2_dict in myDic.items()
for level2_key, level3_dict in level2_dict.items()
for level3_key, values in level3_dict.items()
}
df = pd.DataFrame(restructure_dict).T
print(df)
с выводом:
transmission engine drivetrain wheels_hubs seat_belts_air_bags brakes lights body_paint fuel_system electrical suspension miscellaneous steering accessories-interior accessories-exterior windows_windshield
Acura CL 2003 4 1 NHTSA: 13 NHTSA: 8 NHTSA: 6 NHTSA: 6 NHTSA: 5 NHTSA: 2 NHTSA: 2 NHTSA: 2 NHTSA: 2 NHTSA: 1 NHTSA: 1 NaN NaN NaN
2002 2 NHTSA: 8 NHTSA: 2 NaN NHTSA: 3 NHTSA: 7 NaN NHTSA: 1 NHTSA: 1 NHTSA: 4 NHTSA: 2 NHTSA: 1 NHTSA: 1 NHTSA: 3 NHTSA: 1 NHTSA: 1