2

I know that there have been questions similar to this, but I've not yet been able to figure out how to do what I need to. I'm trying to take some JSON and move it into a Pandas DataFrame.

    {
  "friends": [
    {
      "name": "Joe Jimmy",
      "timestamp": 1541547573
    },
    {
      "name": "Steven Peterson",
      "timestamp": 1541274647
    }
  ]
}

I'd like the corresponding DataFrame to look like this:

     Name               Timestamp   
1 "Joe Jimmy"        "1541547573"
2 "Stephen Peterson" "1541274647"

I think the problem is that first nested "friends," but I'm not sure, as I'm new to JSON (and Pandas, really).

I've tried bringing it in via

 with open('data.json') as f:
   friends = json.load(f)

And then moving it to a dataframe via the Panadas DataFrame constructor, but I'm not getting out anything but this:

{'name': 'Brian B.S. Sheehan', 'timestamp': 15...}
1

2 Answers 2

5

Here is a solution with pandas read_json:

df = pd.read_json(r'C:\path\data.json')
df.friends.apply(pd.Series)

    name            timestamp
0   Joe Jimmy       1541547573
1   Steven Peterson 1541274647
Sign up to request clarification or add additional context in comments.

Comments

0

Try this one. http://pandas.pydata.org/pandas-docs/stable/generated/pandas.io.json.json_normalize.html Or just use read_json function and use groupby method

Try this:

from pandas.io.json import json_normalize
from json import load

data_json = load(open("inp.json", "r"))
print(json_normalize(data_json,'friends'))

2 Comments

I figured it out. I just brought it in and used new_friends = pd.DataFrame(data['friends']) to get the second level into a DF. It's not pretty, but it worked.
I see. Thats great.

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.