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!)

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I used your code and received the following error: ValueError: unexpected character found when decoding array value (2)
@heyou Perhaps this can help you: stackoverflow.com/questions/27240982/…
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.