亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將 JSON 數據轉換為 Pandas DataFrame

將 JSON 數據轉換為 Pandas DataFrame

牛魔王的故事 2023-07-11 15:26:13
"{\".travis.yml\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"Alexandr Tsaplin\"]}, \"count\": 1}, \"fleetspeak/client-mac/com.google.code.fleetspeak.plist\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"mol123\"]}, \"count\": 1}, \"fleetspeak/src/client/client/client.go\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"mol123\"]}, \"count\": 1}, \"fleetspeak/src/client/client_test.go\": {\"auth_count\": 2, \"authors\": {\"py/set\": [\"Ben Galehouse\", \"mol123\"]}, \"count\": 2}, \"fleetspeak/src/client/entry/entry_unix.go\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"mol123\"]}, \"count\": 1}, \"fleetspeak/src/client/entry/entry_windows.go\":我想將 JSON 數據(存儲在 result.json 文件中)轉換為數據幀。我希望我的數據框看起來像所附圖像中的數據框。
查看完整描述

1 回答

?
慕田峪7331174

TA貢獻1828條經驗 獲得超13個贊

您可以將 JSON 讀入數據幀,然后將作者從列表轉換為字符串:


# read json from string `s` to a dataframe

df = (pd.read_json(s).T

    .reset_index()

    .rename(columns={'index': 'Filename', 'auth_count': 'authors_count'}))


# convert lists of authors to comma-separated strings

df['authors'] = df['authors'].apply(lambda x: ', '.join(x['py/set']))


df

輸出:


               Filename authors_count              authors count

0           .travis.yml             1     Alexandr Tsaplin     1

1   fleetspeak/clien...             1               mol123     1

2   fleetspeak/src/c...             1               mol123     1

3   fleetspeak/src/c...             2  Ben Galehouse, m...     2

4   fleetspeak/src/c...             1               mol123     1

5   fleetspeak/src/c...             1               mol123     1

6   fleetspeak/src/c...             1               mol123     1


查看完整回答
反對 回復 2023-07-11
  • 1 回答
  • 0 關注
  • 122 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號