Python – 将JSON列表转换为数据框

huangapple go评论64阅读模式
英文:

Python - Convert json list to a data frame

问题

emptylist = [{'data': {'id': 7478290440, 'version': 0, 'bonus_opening_balance': 7.4, 'cash_opening_balance': 30.83, 'external_round_id': '8997958938', 'game_id': 29788, 'game_session_id': 144418070, 'last_updated_at': '2023-06-29T14:03:03Z', 'started_at': '2023-06-29T14:03:03Z', 'status': 0, 'cash_stake': 0, 'cash_win': 0, 'bonus_stake': 0, 'bonus_win': 0}, 'metadata': {'timestamp': '2023-06-29T12:03:07.699650Z', 'record-type': 'data', 'operation': 'insert', 'partition-key-type': 'schema-table', 'schema-name': 'revolve', 'table-name': 'game_round', 'transaction-id': 103414267563647}}, {'data': {'id': 7478290359, 'version': 2, 'bonus_opening_balance': 0, 'cash_opening_balance': 11.13, 'ended_at': '2023-06-29T14:03:03Z', 'external_round_id': '8997958480', 'game_id': 16210, 'game_session_id': 144418025, 'last_updated_at': '2023-06-29T14:03:03Z', 'started_at': '2023-06-29T14:02:58Z', 'status': 1, 'cash_stake': 0.2, 'cash_win': 0.03, 'bonus_stake': 0, 'bonus_win': 0}, 'metadata': {'timestamp': '2023-06-29T12:03:07.708711Z', 'record-type': 'data', 'operation': 'update', 'partition-key-type': 'schema-table', 'schema-name': 'revolve', 'table-name': 'game_round', 'transaction-id': 103414267564722}}, {'data': {'id': 7478290440, 'version': 1, 'bonus_opening_balance': 7.4, 'cash_opening_balance': 30.83, 'external_round_id': '8997958938', 'game_id': 29788, 'game_session_id': 144418070, 'last_updated_at': '2023-06-29T14:03:03Z', 'started_at': '2023-06-29T14:03:03Z', 'status': 0, 'cash_stake': 0.2, 'cash_win': 0, 'bonus_stake': 0, 'bonus_win': 0}, 'metadata': {'timestamp': '2023-06-29T12:03:07.717096Z', 'record-type': 'data', 'operation': 'update', 'partition-key-type': 'schema-table', 'schema-name': 'revolve', 'table-name': 'game_round', 'transaction-id': 103414267565254}}]
英文:

I have a list that contains nested json data and I would like to convert everything that is in the 'data:' tag into a data frame. The list contains multiple records inside the

emptylist = [{'data': {'id': 7478290440, 'version': 0, 'bonus_opening_balance': 7.4, 'cash_opening_balance': 30.83, 'external_round_id': '8997958938', 'game_id': 29788, 'game_session_id': 144418070, 'last_updated_at': '2023-06-29T14:03:03Z', 'started_at': '2023-06-29T14:03:03Z', 'status': 0, 'cash_stake': 0, 'cash_win': 0, 'bonus_stake': 0, 'bonus_win': 0}, 'metadata': {'timestamp': '2023-06-29T12:03:07.699650Z', 'record-type': 'data', 'operation': 'insert', 'partition-key-type': 'schema-table', 'schema-name': 'revolve', 'table-name': 'game_round', 'transaction-id': 103414267563647}}, {'data': {'id': 7478290359, 'version': 2, 'bonus_opening_balance': 0, 'cash_opening_balance': 11.13, 'ended_at': '2023-06-29T14:03:03Z', 'external_round_id': '8997958480', 'game_id': 16210, 'game_session_id': 144418025, 'last_updated_at': '2023-06-29T14:03:03Z', 'started_at': '2023-06-29T14:02:58Z', 'status': 1, 'cash_stake': 0.2, 'cash_win': 0.03, 'bonus_stake': 0, 'bonus_win': 0}, 'metadata': {'timestamp': '2023-06-29T12:03:07.708711Z', 'record-type': 'data', 'operation': 'update', 'partition-key-type': 'schema-table', 'schema-name': 'revolve', 'table-name': 'game_round', 'transaction-id': 103414267564722}}, {'data': {'id': 7478290440, 'version': 1, 'bonus_opening_balance': 7.4, 'cash_opening_balance': 30.83, 'external_round_id': '8997958938', 'game_id': 29788, 'game_session_id': 144418070, 'last_updated_at': '2023-06-29T14:03:03Z', 'started_at': '2023-06-29T14:03:03Z', 'status': 0, 'cash_stake': 0.2, 'cash_win': 0, 'bonus_stake': 0, 'bonus_win': 0}, 'metadata': {'timestamp': '2023-06-29T12:03:07.717096Z', 'record-type': 'data', 'operation': 'update', 'partition-key-type': 'schema-table', 'schema-name': 'revolve', 'table-name': 'game_round', 'transaction-id': 103414267565254}}]
print(emptylist)

答案1

得分: 0

尝试这个

import pandas as pd
emptylist = [{'data': ... }]
data_list = [record['data'] for record in emptylist]
df = pd.DataFrame(data_list)
print(df)
英文:

Try this

import pandas as pd
emptylist = [{'data': ... }]
data_list = [record['data'] for record in emptylist]
df = pd.DataFrame(data_list)
print(df)

答案2

得分: 0

pd.DataFrame(emptylist)[ "data" ].apply(pd.Series)

英文:

You could create a DataFrame from the list and use apply on the "data" series to make it a DataFrame:

pd.DataFrame(emptylist)["data"].apply(pd.Series)

huangapple
  • 本文由 发表于 2023年6月29日 20:15:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580964.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定