Gabe H. Coud
Gabe H. Coud

Reputation: 175

Pandas: Convert JSON to Pandas Dataframe

Experts,

I am having issues parsing Json to Pandas and then save it in CSV format.

data2 = {"date":"2018-01-02","data":{"AAPL":{"open":"170.16","close":"172.26","high":"172.30","low":"169.26","volume":"25555934"},"MSFT":{"open":"86.13","close":"85.95","high":"86.31","low":"85.50","volume":"22483797"}}}

If I try :

df = pd.DataFrame.from_dict(json_normalize(data2), orient='columns')
print(df)

Everything gets printed in a single line :

enter image description here

If I do :

jdata = json.loads(data2)
df = pd.DataFrame(jdata)
print(df.T)

I get an error : TypeError: the JSON object must be str, bytes or bytearray, not 'dict'

I am looking to print it in the following table format so I can then save it as csv :

Date        Data    Open    Close   High    Low     Volume
2018-01-02  AAPL    170.16  172.26  172.30  169.26  25555934
2018-01-02  MSFT    86.13   85.95   86.31   85.50   22483797

What is the correct way to achieve my goal ?

Thanks !!

Upvotes: 2

Views: 8077

Answers (2)

Zito Relova
Zito Relova

Reputation: 1041

You can use apply to turn the dict keys into pandas Series

df = pd.DataFrame.from_dict(data2)
df = pd.concat([df['date'],df['data'].apply(pd.Series)], axis=1)
print(df)

            date    open   close    high     low    volume
AAPL  2018-01-02  170.16  172.26  172.30  169.26  25555934
MSFT  2018-01-02   86.13   85.95   86.31   85.50  22483797

Upvotes: 5

BENY
BENY

Reputation: 323226

I will using your original output and modify it

s=pd.DataFrame(data2)
pd.concat([s.drop('data',1),pd.DataFrame(s.data.tolist(),index=s.index)],1)
            date   close    high     low    open    volume
AAPL  2018-01-02  172.26  172.30  169.26  170.16  25555934
MSFT  2018-01-02   85.95   86.31   85.50   86.13  22483797

Upvotes: 1

Related Questions