Python Forum
Converting a json file to a dataframe with rows and columns
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Converting a json file to a dataframe with rows and columns
#4
That depends on what is "meta"? Is meta a user that has an id == 178054?
import json
import pandas as pd

json_string = '[{"meta": {"user_id": "178054", "level_id": 1}}, {"beta": {"user_id": "178055", "level_id": 2}}]'
game_data = json.loads(json_string)
names = [key for player in game_data for key, value in player.items()]
stats = [value for player in game_data for key, value in player.items()]
game_df = pd.DataFrame(stats, index=names)
print(game_df)
Output:
user_id level_id meta 178054 1 beta 178055 2
Or is meta something else? Should users be saved like this?
import json
import pandas

json_string = '[{"meta": {"user_id": ["178054", "178055"], "level_id": [1, 2]}}]'
game_data = json.loads(json_string)[0]["meta"]
game_df = pandas.DataFrame(game_data)
print(game_df)
Output:
user_id level_id 0 178054 1 1 178055 2
Seeing a tiny snapshot of your json is not enough. You need to tell us what the json file mans.
Reply


Messages In This Thread
RE: Converting a json file to a dataframe with rows and columns - by deanhystad - Jan-28-2023, 09:19 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Photo Converting Pandas DataFrame to a table of hourly blocks Abedin 1 692 Apr-24-2025, 01:05 PM
Last Post: snippsat
  Running search/replace across Polars dataframe columns efficiently hobbycoder 3 2,596 Oct-28-2024, 03:18 AM
Last Post: hobbycoder
  JSON File - extract only the data in a nested array for CSV file shwfgd 2 1,204 Aug-26-2024, 10:14 PM
Last Post: shwfgd
  docx file to pandas dataframe/excel iitip92 1 3,066 Jun-27-2024, 05:28 AM
Last Post: Pedroski55
  encrypt data in json file help jacksfrustration 1 2,437 Mar-28-2024, 05:16 PM
Last Post: deanhystad
  Converting column of values into muliple columns of counts highland44 0 956 Feb-01-2024, 12:48 AM
Last Post: highland44
  Converting .txt to .csv file SunWers 21 22,668 Jan-20-2024, 10:03 AM
Last Post: Larz60+
  Create Choices from .ods file columns cspower 3 1,938 Dec-28-2023, 09:59 PM
Last Post: deanhystad
  parse json field from csv file lebossejames 4 2,206 Nov-14-2023, 11:34 PM
Last Post: snippsat
  Create csv file with 4 columns for process mining thomaskissas33 3 1,939 Nov-06-2023, 09:36 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020