因此,資料將[outbreakMap]列為pandas series。我將資料片段重新創建為下面的 df:
初始資料:

data = {0: {'id': '53545',
'latitude': -1.5168500000000003,
'longitude': 105.2985,
'outbreakMap': {'ob_102795': {'nationalObReference': '403/KPTS/05/22',
'oieReference': 'ob_102795',
'outbreakStartDate': '2022-04-12'},
'ob_102796': {'nationalObReference': '404/KPTS/05/22',
'oieReference': 'ob_102796',
'outbreakStartDate': '2022-04-22'}}},
1: {'id': '53709',
'latitude': 36.42056,
'longitude': 139.9085,
'outbreakMap': {'ob_101531': {'oieReference': 'ob_101531',
'outbreakStartDate': '2022-04-12'},
'ob_101644': {'oieReference': 'ob_101644',
'outbreakStartDate': '2022-04-14'},
'ob_102290': {'oieReference': 'ob_102290',
'outbreakStartDate': '2022-04-21'},
'ob_100121': {'oieReference': 'ob_100121',
'outbreakStartDate': '2022-03-24'},
'ob_102949': {'oieReference': 'ob_102949',
'outbreakStartDate': '2022-05-09'}}}}
import pandas as pd
df = pd.DataFrame.from_dict(data, orient='index')
目標:將字典解壓縮成長格式,如下所示:

謝謝你。
uj5u.com熱心網友回復:
[編輯]
我想這就是你想要的。
def into_long(outbreakMap):
'''
Extracting key, oieReference, and outbreakStartDate
from outbreakMap
'''
keys = outbreakMap.keys()
# return list of tuples
return [(
key,
outbreakMap[key]['oieReference'],
outbreakMap[key]['outbreakStartDate']
) for key in keys]
df['outbreakMap_tuple'] = df['outbreakMap'].apply(lambda x: into_long(x))
# list to multiple rows
# further reading: https://stackoverflow.com/questions/39954668/how-to-convert-column-with-list-of-values-into-rows-in-pandas-dataframe
df = df.explode('outbreakMap_tuple')
# tuples to multiple columns
# further reading: https://stackoverflow.com/questions/29550414/how-can-i-split-a-column-of-tuples-in-a-pandas-dataframe
df[['outbreakMap', 'oieReference', 'outbreakStartDate']] = pd.DataFrame(df['outbreakMap_tuple'].tolist(), index=df.index)
df.drop('outbreakMap_tuple', axis=1, inplace=True)
# this is optional
df.reset_index(drop=True, inplace=True)
結果看起來像

我確信有更好的方法。這只是其中之一。
uj5u.com熱心網友回復:
雖然您可以構建 DataFrame 并在之后處理列,但如果您修改以將所需的鍵包含在內部 dicts 中,然后構建 DataFrame 和添加的列outbreakMap,則可能會更容易和更有效:dataexplode
for v in data.values():
outbreakMap = v.pop('outbreakMap')
v['outbreak'] = list(outbreakMap)
v['outbreak_information'] = list(outbreakMap.values())
out = pd.DataFrame.from_dict(data, orient='index').explode(['outbreak','outbreak_information'])
輸出:
id latitude longitude outbreak outbreak_information
0 53545 -1.51685 105.2985 ob_102795 {'nationalObReference': '403/KPTS/05/22', 'oie...
0 53545 -1.51685 105.2985 ob_102796 {'nationalObReference': '404/KPTS/05/22', 'oie...
1 53709 36.42056 139.9085 ob_101531 {'oieReference': 'ob_101531', 'outbreakStartDa...
1 53709 36.42056 139.9085 ob_101644 {'oieReference': 'ob_101644', 'outbreakStartDa...
1 53709 36.42056 139.9085 ob_102290 {'oieReference': 'ob_102290', 'outbreakStartDa...
1 53709 36.42056 139.9085 ob_100121 {'oieReference': 'ob_100121', 'outbreakStartDa...
1 53709 36.42056 139.9085 ob_102949 {'oieReference': 'ob_102949', 'outbreakStartDa...
uj5u.com熱心網友回復:
由于某些資料在多行中重復,我認為撰寫 python 表然后將其轉換為資料框更容易。資料框在首次創建時需要知道完整的行數 - 所以 pandas 無論如何都需要制作一個中間串列。
它只是一個嵌套的 for 回圈,將行放入主串列中,將外部回圈中的資料復制到內部回圈中。資料是通過列名串列從字典中提取的,因此我們可以獲得每一行的正確順序。
我注意到breakupMap 似乎是重復的,因此跳過了它,并且所有資料中都沒有nationalObReference。我還將代碼放入一個函式中,這是一種丟棄計算完成后不需要的中間資料的簡單方法。
data = {0: {'id': '53545',
'latitude': -1.5168500000000003,
'longitude': 105.2985,
'outbreakMap': {'ob_102795': {'nationalObReference': '403/KPTS/05/22',
'oieReference': 'ob_102795',
'outbreakStartDate': '2022-04-12'},
'ob_102796': {'nationalObReference': '404/KPTS/05/22',
'oieReference': 'ob_102796',
'outbreakStartDate': '2022-04-22'}}},
1: {'id': '53709',
'latitude': 36.42056,
'longitude': 139.9085,
'outbreakMap': {'ob_101531': {'oieReference': 'ob_101531',
'outbreakStartDate': '2022-04-12'},
'ob_101644': {'oieReference': 'ob_101644',
'outbreakStartDate': '2022-04-14'},
'ob_102290': {'oieReference': 'ob_102290',
'outbreakStartDate': '2022-04-21'},
'ob_100121': {'oieReference': 'ob_100121',
'outbreakStartDate': '2022-03-24'},
'ob_102949': {'oieReference': 'ob_102949',
'outbreakStartDate': '2022-05-09'}}}}
import pandas as pd
def data_to_df(data):
common_cols = ["id", "latitude", "longitude"]
outbreak_cols = ["nationalObReference", "oieReference", "outbreakStartDate"]
table = []
for index, common in data.items():
common_row = [common[name] for name in common_cols]
for ob, ref in common["outbreakMap"].items():
row = common_row [ref.get(name, None) for name in outbreak_cols]
table.append(row)
df = pd.DataFrame(table, columns=common_cols outbreak_cols)
return df
df = data_to_df(data)
uj5u.com熱心網友回復:
您可以使用串列理解:
lst_3d = [
[
[data[n]['id'],
data[n]['latitude'],
data[n]['longitude'],
data[n]['outbreakMap'][i]['oieReference'],
data[n]['outbreakMap'][i]['outbreakStartDate']
]
for i in data[n]['outbreakMap']]
for n in data]
#convert to 2d list
lst = [elem for subl in lst_3d for elem in subl]
df = pd.DataFrame(lst, columns=['id', 'latitude', 'longitude', 'oieReference', 'outbreakStartDate'])
df
Out[1]:
id latitude longitude oieReference outbreakStartDate
0 53545 -1.51685 105.2985 ob_102795 2022-04-12
1 53545 -1.51685 105.2985 ob_102796 2022-04-22
2 53709 36.42056 139.9085 ob_101531 2022-04-12
3 53709 36.42056 139.9085 ob_101644 2022-04-14
4 53709 36.42056 139.9085 ob_102290 2022-04-21
5 53709 36.42056 139.9085 ob_100121 2022-03-24
6 53709 36.42056 139.9085 ob_102949 2022-05-09
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/472481.html
上一篇:加快從熊貓資料框中讀取內容的速度
