處理巢狀資料欄位

使用 pandas 重塑資料

Maria Eugenia Inzaugarat

Data Scientist

複習

  • 使用 json_normalize() 將巢狀 JSON 讀入 DataFrame。
使用 pandas 重塑資料

欄位中的巢狀資料

writers = ["Mary Shelley", "Ernest Hemingway"]
books = ["{'title': 'Frankenstein', 'year': 1818}", 
        "{'title': 'The Old Man and the Sea', 'year': 1951}"]

collection = pd.DataFrame( )
使用 pandas 重塑資料

欄位中的巢狀資料

writers = ["Mary Shelley", "Ernest Hemingway"]
books = ["{'title': 'Frankenstein', 'year': 1818}", 
        "{'title': 'The Old Man and the Sea', 'year': 1951}"]

collection = pd.DataFrame(dict( ))
使用 pandas 重塑資料

欄位中的巢狀資料

writers = ["Mary Shelley", "Ernest Hemingway"]
books = ['{"title": "Frankenstein", "year": "1818"}',
         '{"title": "The Old Man and the Sea", "year":"1951"}']

collection = pd.DataFrame(dict(writers=writers, books=books))
collection
            writers                                               books
0      Mary Shelley             {'title': 'Frankenstein', 'year': 1818}
1  Ernest Hemingway  {'title': 'The Old Man and the Sea', 'year': 1951}
使用 pandas 重塑資料

轉換巢狀資料

import json

books = collection['books']
使用 pandas 重塑資料

轉換巢狀資料

import json

books = collection['books'].apply( )
使用 pandas 重塑資料

轉換巢狀資料

import json

books = collection['books'].apply(json.loads)
使用 pandas 重塑資料

轉換巢狀資料

import json

books = collection['books'].apply(json.loads).apply(pd.Series)
books
                     title  year
0             Frankenstein  1818
1  The Old Man and the Sea  1951
使用 pandas 重塑資料

再串回去

collection = collection.drop(columns='books')

pd.concat([collection, books], axis=1)
            writers                    title  year
0      Mary Shelley             Frankenstein  1818
1  Ernest Hemingway  The Old Man and the Sea  1951
使用 pandas 重塑資料

序列化巢狀資料

import json
books = collection['books'].apply(json.loads)



使用 pandas 重塑資料

序列化巢狀資料

import json
books = collection['books'].apply(json.loads).to_list()

books_dump = json.dumps(books)
new_books = pd.read_json(books_dump)
new_books
                     title  year
0             Frankenstein  1818
1  The Old Man and the Sea  1951
使用 pandas 重塑資料

序列化巢狀資料

pd.concat([collection['writers'], new_books], axis=1)
            writers                    title  year
0      Mary Shelley             Frankenstein  1818
1  Ernest Hemingway  The Old Man and the Sea  1951
使用 pandas 重塑資料

一起來練習吧!

使用 pandas 重塑資料

Preparing Video For Download...