Home > database >  I get this error : If using all scalar values must pass index . I'm writing a basic jason dataf
I get this error : If using all scalar values must pass index . I'm writing a basic jason dataf

Time:11-11

This is my code:

data ='{"name": " sani", "address": " Czech", "Age": "10", "Gender": "Female"}'
pd.read_json(data) ( I cannot execute this line, it shows that error)

i tried adding Index= 0 and it didn't work as well

CodePudding user response:

Instead of just passing the values as just plain strings, use lists instead.

Here is your example fixed:

import pandas as pd
data ='{"name": [" sani"], "address": [" Czech"], "Age": ["10"], "Gender": ["Female"]}'

pd.read_json(data)

CodePudding user response:

because your data's type is string not dictionary. First convert string to dictionary:

import json
data ='{"name": " sani", "address": " Czech", "Age": "10", "Gender": "Female"}'
data=json.loads(data)

then use json_normalize for convert to dataframe:

df = pd.json_normalize(data)

full code:

import pandas as pd
import json

data ='{"name": " sani", "address": " Czech", "Age": "10", "Gender": "Female"}'
data=json.loads(data)
df=pd.json_normalize(data)

print(df)
'''
    name    address Age Gender
0    sani    Czech  10  Female

'''
  • Related