簡體   English   中英

有沒有辦法從列表中的字典創建 dataframe?

[英]Is there a way to create a dataframe from a Dictionary inside a list?

我有一個名為 layout 的 dataframe ,它有一個名為 layout(是同名)的列,其中包含這樣的嵌套數據。

[{'floors': [{'floor_id': 1024,
        'floor_name': 'Apartment',
        'rooms': [{'devices': [1017,
           1021,
           1032,
           1038,
           1041,
           1048,
           1052,
           1060,
           1016,
           1062,
           1069,
           1048],
          'room_id': 1025,
          'room_name': 'Living room'},
         {'devices': [1018,
           1022,
           1037,
           1047,
           1067,
           1070,
           1073,
           1079,
           1080,
           1081,
           1041,
           1045,
           1047],
          'room_id': 1026,
          'room_name': 'Kitchen'},
         {'devices': [1034,
           1036,
           1046,
           1050,
           1049,
           1055,
           1071,
           1074,
           1076,
           1044,
           1046],
          'room_id': 1027,
          'room_name': 'Bathroom'},
         {'devices': [1023, 1033, 1045, 1054, 1075, 1042],
          'room_id': 1028,
          'room_name': 'Bedroom 1'},
         {'devices': [1020, 1051, 1053, 1066, 1068, 1077, 1078, 1043],
          'room_id': 1029,
          'room_name': 'Bedroom 2'}]},
       {'floor_id': 1021,
        'floor_name': 'First',
        'rooms': [{'room_id': 1023, 'room_name': 'Kitchen', 'devices': [1019]},
         {'room_id': 1024, 'room_name': 'Beedroom', 'devices': [1030]},
         {'room_id': 1025, 'room_name': 'Store', 'devices': [1035]}]},
       {'floor_id': 1026,
        'floor_name': 'Ground',
        'rooms': [{'room_id': 1027, 'room_name': 'Hall', 'devices': [1032, 1033]},
         {'room_id': 1028, 'room_name': 'Gallery', 'devices': [1022, 1034]}]}]}]

有沒有辦法把它放在一個 dataframe 中? 我希望像 floor_id 和 floor_name 值應該對所有行通用。 我遇到的問題是處理關鍵的“房間”。 我希望 dataframe 直接有一個名為 devices 的列(在房間內),並在設備中包含數字作為行。 然后最后一列將是 room_id 和 room_name

floor_id 樓層名稱 設備 room_id 房間名
1024 公寓 1017 1025 客廳
1024 公寓 1021 1025 客廳
1024 公寓 1032 1025 客廳
…………
1024 公寓 1068 1029 卧室 2
1024 公寓 1077 1029 卧室 2
1024 公寓 1078 1029 卧室 2
1024 公寓 1043 1029 卧室 2

我嘗試使用

    pd.DataFrame(layout.iloc[0]['layout'])

但我明白了

樓層 0 [{'floor_id': 1024, 'floor_name': '公寓',...

您應該將嵌套字典分解為具有理解的平面字典:

data = layout.iloc[0]['layout']
df = pd.DataFrame([{'floor_id': floor['floor_id'], 'floor_name': floor['floor_name'],
       'devices': device, 'room_id': room['room_id'],
       'room_name': room['room_name']} for floor in data[0]['floors']
      for room in floor['rooms'] for device in room['devices']])

它按預期給出:

    floor_id floor_name  devices  room_id    room_name
0       1024  Apartment     1017     1025  Living room
1       1024  Apartment     1021     1025  Living room
2       1024  Apartment     1032     1025  Living room
3       1024  Apartment     1038     1025  Living room
4       1024  Apartment     1041     1025  Living room
5       1024  Apartment     1048     1025  Living room
6       1024  Apartment     1052     1025  Living room
7       1024  Apartment     1060     1025  Living room
8       1024  Apartment     1016     1025  Living room
9       1024  Apartment     1062     1025  Living room
10      1024  Apartment     1069     1025  Living room
11      1024  Apartment     1048     1025  Living room
12      1024  Apartment     1018     1026      Kitchen
13      1024  Apartment     1022     1026      Kitchen
...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM