試圖解包(我認為被稱為)一個多嵌套字典串列以實作以下輸出:
subject_code module notes topics start end faculty_id faculty_leader
0 90 ABC Board Spring 19 refer to guidance: https://nothing.com/general... [1A, 2G, 3S] 1643847382 1644279382 nothing Steve Finn
1 80 ABC Board Spring 18 refer to guidance: https://nothing.com/general... [1D, 5F, 2B] 1643847382 1644279382 nothing Steve Finn
以下是原始資料和我未能成功實作上述輸出的嘗試:
import pandas as pd
data_ = [
{
"subject_code": 90, "module": "ABC Board Spring 19",
"notes" : "refer to guidance: https://nothing.com/general-information/overleaf",
"topics" : ['1A', '2G', '3S'], "start":1643847382,"end":1644279382,
"faculty":{"id":"nothing","leader":"Steve Finn"}
},
{
"subject_code": 80, "module": "ABC Board Spring 18",
"notes" : "refer to guidance: https://nothing.com/general-information/overleaf",
"topics" : ['1D', '5F', '2B'], "start":1643847382,"end":1644279382,
"faculty":{"id":"nothing","leader":"Steve Finn"}}]
df = pd.DataFrame(data_ )
print(df)
但是,最后一列faculty未按預期顯示....
subject_code module notes topics start end faculty
0 90 ABC Board Spring 19 refer to guidance: https://nothing.com/general... [1A, 2G, 3S] 1643847382 1644279382 {'id': 'nothing', 'leader': 'Steve Finn'}
1 80 ABC Board Spring 18 refer to guidance: https://nothing.com/general... [1D, 5F, 2B] 1643847382 1644279382 {'id': 'nothing', 'leader': 'Steve Finn'}
faculty我不想顯示為包含資料字典的單個列,而是將其拆分為單獨的列,命名為字典名稱和鍵...
以下是該執行緒開頭所示的所需輸出:
subject_code module notes topics start end faculty_id faculty_leader
0 90 ABC Board Spring 19 refer to guidance: https://nothing.com/general... [1A, 2G, 3S] 1643847382 1644279382 nothing Steve Finn
1 80 ABC Board Spring 18 refer to guidance: https://nothing.com/general... [1D, 5F, 2B] 1643847382 1644279382 nothing Steve Finn
uj5u.com熱心網友回復:
你可以只使用json_normalize. 將列重命名為所需的名稱是我留給讀者的練習。
pd.json_normalize(data_)
subject_code module notes topics start end faculty.id faculty.leader
0 90 ABC Board Spring 19 refer to guidance: https://nothing.com/general... [1A, 2G, 3S] 1643847382 1644279382 nothing Steve Finn
1 80 ABC Board Spring 18 refer to guidance: https://nothing.com/general... [1D, 5F, 2B] 1643847382 1644279382 nothing Steve Finn
uj5u.com熱心網友回復:
您可以在“faculty”列上使用 DataFrame 建構式,join并將生成的 DataFrame 回傳到df(使用add_prefix,您可以添加“faculty”):
out = df.join(pd.DataFrame(df['faculty'].tolist()).add_prefix('faculty_')).drop(columns='faculty')
輸出:
subject_code module \
0 90 ABC Board Spring 19
1 80 ABC Board Spring 18
notes topics \
0 refer to guidance: https://nothing.com/general... [1A, 2G, 3S]
1 refer to guidance: https://nothing.com/general... [1D, 5F, 2B]
start end faculty_id faculty_leader
0 1643847382 1644279382 nothing Steve Finn
1 1643847382 1644279382 nothing Steve Finn
uj5u.com熱心網友回復:
您可以在傳遞給 DataFrame 建構式之前展平“教師”
例如:
for d in data_:
faculty = d.pop("faculty")
d["faculty_id"] = faculty["id"]
d["faculty_leader"] = faculty["leader"]
然后將 data_ 傳遞給 DataFrame 建構式
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/425654.html
上一篇:如何從熊貓串列中創建接觸點資料框
