簡體   English   中英

從 pandas json 文件中提取數據

[英]Extraction from data from pandas json file

我有以下 json 數據文件,我已將其轉換為 pandas dataframe。 欄目如下

Index(['id', 'title', 'abstract', 'content', 'metadata'], dtype='object')

我對“元數據”列特別感興趣,該列的元素看起來像

df_json.loc[78, 'metadata']
"{'classification': {'name': 'Manufacturing, Transport & Logistics'}, 'subClassification': {'name': 'Warehousing, Storage & Distribution'}, 'area': {'name': 'Southern Suburbs & Logan'}, 'location': {'name': 'Brisbane'}, 'suburb': {'name': 'Milton'}, 'workType': {'name': 'Casual/Vacation'}}"

所以我想讓列從“元數據”列中提取信息,例如位置。 我不確定如何提取它並將其放在同一個 json 文件旁邊,並添加了諸如位置等列。

    id  title   abstract    content metadata    clean_content
0   38915469    Recruitment Consultant  We are looking for someone to focus purely on ...   <HTML><p>Are you looking to join a thriving bu...   {'standout': {'bullet1': 'Join a Sector that i...   Are you looking to join a thriving business th...
1   38934839    Computers Salesperson - Coburg  Passionate about exceptional customer service?...   <HTML><p>&middot;&nbsp;&nbsp;Casual hours as r...   {'additionalSalaryText': 'Attractive Commissio...   middotnbspnbspCasual hours as required transit...
2   38946054    Senior Developer | SA   Readifarians are known for discovering the lat...   <HTML><p>Readify helps organizations 

 you can use pandas.json_normalize 

應用於你的字符串

 pd.json_normalize(eval(json_string)) 

 #o/p

在此處輸入圖像描述

如果這對您有用,那么您可以嘗試

 df["metadata"].apply(lambda x: pd.json_normalize(eval(x)))

暫無
暫無

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

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