Pandas – ValueError: If using all scalar values, you must pass an index

Reading json file using Pandas read_json can fail with “ValueError: If using all scalar values, you must pass an index”. Let see with an example –

cat a.json
{
  "creator": "CaptainAmerica",
  "last_modifier": "NickFury",
  "title": "Captain America: The First Avenger",
  "view_count": 12000
}
>>> import pandas as pd
>>> import glob
>>> for f in glob.glob('*.json'):
...     print(f)
...
b.json
c.json
a.json
>>> pd.read_json('a.json')
Traceback (most recent call last):
  File "", line 1, in 
  .......
  raise ValueError('If using all scalar values, you must pass'
ValueError: If using all scalar values, you must pass an index

Pandas expects the value to be a list or dict. For example –

cat al.json
{
  "creator": ["CaptainAmerica"],
  "last_modifier": ["NickFury"],
  "title": ["Captain America: The First Avenger"],
  "view_count": [12000]
}

>>> pd.read_json('al.json')
          creator last_modifier                               title  \
0  CaptainAmerica      NickFury  Captain America: The First Avenger

   view_count
0       12000
>>>

Pandas Series is a one-dimensional labeled array capable of holding any data type whereas DataFrame is two dimensional object that can have columns with potential different types. Frame is default.

Converting to use Series

>>> pd.read_json('a.json', typ='series')
creator                              CaptainAmerica
last_modifier                              NickFury
title            Captain America: The First Avenger
view_count                                    12000
dtype: object
>>>

>>> df = pd.DataFrame([pd.read_json('a.json',  typ='series')])
>>> df
          creator last_modifier                               title  \
0  CaptainAmerica      NickFury  Captain America: The First Avenger

   view_count
0       12000

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s