0

I have a following JSON file (see link below) that I read from the web. I would like to search for a specific string and return the index of it.

Also, if there is a better way to read and manipulate JSON data other than how I use it here, I would like to know. Thanks!

JSON FILE

Here's how I read the file:

import json
import pandas as pd
from pandas import DataFrame, Series
#-------------------------------------------------
# READING IN THE JSON FILE 
#-------------------------------------------------
path = 'ads_nasa.txt'
records = [json.loads(line) for line in open(path)]
frame = DataFrame(records)

I tried to use the following but it does not work:

frame.author[0].find('Deshpande, R.')

How do I search for a string and find its index in a Pandas Core Series?

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
Rohit
  • 5,840
  • 13
  • 42
  • 65
  • Also, when I use pd.read_json(path) I get an error, "arrays must all be same length". So, I resorted to use the above method. – Rohit Feb 22 '14 at 16:38

1 Answers1

2

In this case use the index method of list objects:

frame.author[0].index('Deshpande, R.')

I would recommend looking into filtering your JSON prior to putting it into a DataFrame so that you can take advantage of all the indexing operations that are available in pandas. Check out this question and answer for an example: Create a Pandas DataFrame from deeply nested JSON.

Community
  • 1
  • 1
Phillip Cloud
  • 24,919
  • 11
  • 68
  • 88