1

I am trying to read a json file in Python and convert it into a dataframe. The problem is that my json file has several json objects inside. The structure of my json is like this:

{"Temp":"2,3", "OutsideTemp" : "3,4",...}
{"Temp":"3,2", "OutsideTemp" : "4,4",...}
{"Temp":"2,8", "OutsideTemp" : "3,7",...}
...

I've tried using json lines and pandas.read_json but only got errors. (I'm noob at python as you can see, help me!)

heyou
  • 13
  • 4

2 Answers2

1

You have a JSON Lines format text file, so set lines=True:

import pandas as pd

df = pd.read_json('data.txt', lines=True)
print(df)

Output

  Temp OutsideTemp
0  2,3         3,4
1  3,2         4,4
2  2,8         3,7

From the documentation:

lines bool, default False
Read the file as a json object per line.

Note that you have to change data.txt in the snippet above to your actual file name.

Dani Mesejo
  • 61,499
  • 6
  • 49
  • 76
  • I used your code and received the following error: ValueError: unexpected character found when decoding array value (2) – heyou Nov 20 '20 at 11:54
  • @heyou Perhaps this can help you: https://stackoverflow.com/questions/27240982/valueerror-when-using-pandas-read-json – Dani Mesejo Nov 20 '20 at 11:56
0
import pandas as pd

df = pd.read_json('data.txt', lines=True)
df = df.apply(lambda x:x.str.replace(',','.')).astype(float)
print(df.info())
df
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 2 columns):
Temp           3 non-null float64
OutsideTemp    3 non-null float64
dtypes: float64(2)
memory usage: 176.0 bytes
None

Output

ulmefors
  • 516
  • 3
  • 11