英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论