0

I have a text file with the following structure:

[{"name":"sfdsf","stat1":304,"stat2":4.4,"stat3":68},
 {"name":"Swejkl","stat1":211,"stat2":5.0,"stat3":42},
 {"name":"utyuytu","stat1":342,"stat2":4.3,"stat3":79},
 {"name":"vbnvbn","stat1":198,"stat2":3.1,"stat3":63},  etcetera...]

Could someone point me in the right direction for the best way to get this info from a text file into a panda data frame? I want to make columns: "name" "stat1" "stat2" "stat3".

I was thinking of using a for loop to read through each line and extract the information and put it into the data frame line by line.

Is there a better way? Is it better to read the commas as separators. And then do another step where I use the colon as separator? And then get rid of the names of the columns?

When the file is being read does each line get read into the dataframe in new row?

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 3
    Does this answer your question? [JSON to pandas DataFrame](https://stackoverflow.com/questions/21104592/json-to-pandas-dataframe) – Celius Stingher Jul 09 '20 at 00:48

1 Answers1

1

Pandas has a built-in JSON parser, e.g. here:

import pandas as pd
df = pd.read_json(r'myfilepath')
display(df)

enter image description here

Celius Stingher
  • 17,835
  • 6
  • 23
  • 53
a11
  • 3,122
  • 4
  • 27
  • 66