city_data = {
'San Francisco': {'x': [1, 2, 3], 'y': [4, 1, 2]},
'Montreal': {'x': [1, 2, 3], 'y': [2, 4, 5]},
'New York City': {'x': [1, 2, 3], 'y': [2, 2, 7]},
'Cincinnati': {'x': [1, 2, 3], 'y': [1, 0, 2]},
'Toronto': {'x': [1, 2, 3], 'y': [4, 7, 3]},
'Ottawa': {'x': [1, 2, 3], 'y': [2, 3, 3]}
}
< /code>
Такой, что DataFrame выглядит так: < /p>
city | x | y
San Francisco | 1 | 4
San Francisco | 2 | 1
San Francisco | 3 | 2
...
< /code>
Использование решения, которое я обнаружил здесь, разворачивает вложенный словарь с списками в DataFrame Pandas, я попробовал: < /p>
data = city_data
def unroll(data):
if isinstance(data, dict):
for key, value in data.items():
# Recursively unroll the next level and prepend the key to each row.
for row in unroll(value):
yield [key] + row
if isinstance(data, list):
# This is the bottom of the structure (defines exactly one row).
yield data
df = pd.DataFrame(list(unroll(nested_dict)))
df.rename(columns=lambda i: 'col{}'.format(i+1))
city | x | y San Francisco | 1 | 4 San Francisco | 2 | 1 San Francisco | 3 | 2 ... < /code>
Использование решения, которое я обнаружил здесь, разворачивает вложенный словарь с списками в DataFrame Pandas, я попробовал: < /p>
data = city_data
def unroll(data): if isinstance(data, dict): for key, value in data.items(): # Recursively unroll the next level and prepend the key to each row. for row in unroll(value): yield [key] + row if isinstance(data, list): # This is the bottom of the structure (defines exactly one row). yield data
У меня есть данные о погоде в дикте Python, который я пытаюсь конвертировать в DF Pandas. (Оттуда я загрузите его в SQLServer, но у меня есть эта часть)
my_dict = {
'data.outdoor.temperature': {'unit': '℃', 'list':
{'datetime.datetime(2025, 4, 23,...