In [1]:
import pandas as pd
import json
In [2]:
s = '{"col1":{"row1":1,"row2":2,"row3":3},"col2":{"row1":"a","row2":"x","row3":"\u3042"}}'
In [3]:
df_s = pd.read_json(s)
In [4]:
print(df_s)
In [5]:
s_single_quote = "{'col1':{'row1':1,'row2':2,'row3':3},'col2':{'row1':'a','row2':'x','row3':'\u3042'}}"
In [6]:
# df_s_single_quote = pd.read_json(s_single_quote)
# ValueError: Expected object or value
In [7]:
print(pd.read_json(s_single_quote.replace("'", '"')))
In [8]:
df_f = pd.read_json('data/src/sample_from_pandas_columns.json')
In [9]:
print(df_f)
In [10]:
df_gzip = pd.read_json('data/src/sample_from_pandas_columns.gz', compression='infer')
In [11]:
print(df_gzip)
In [12]:
df_s_index = pd.read_json(s, orient='index')
In [13]:
print(df_s_index)
In [14]:
# df_s_split = pd.read_json(s, orient='split')
# ValueError: JSON data had unexpected key(s): col2, col1
In [15]:
s_jsonl = '''{"col1":1,"col2":"a"}
{"col1":2,"col2":"x"}
{"col1":3,"col2":"\u3042"}'''
In [16]:
print(s_jsonl)
In [17]:
df_s_jsonl = pd.read_json(s_jsonl, orient='records', lines=True)
In [18]:
print(df_s_jsonl)
In [19]:
s_nested = '{"OTHER": "x", "DATA": {"col1":{"row1":1,"row2":2},"col2":{"row1":"a","row2":"x"}}}'
In [20]:
print(pd.read_json(s_nested))
In [21]:
d = json.loads(s_nested)
In [22]:
print(d)
In [23]:
print(type(d))
In [24]:
d_target = d['DATA']
In [25]:
print(d_target)
In [26]:
print(type(d_target))
In [27]:
s_target = json.dumps(d_target)
In [28]:
print(s_target)
In [29]:
print(type(s_target))
In [30]:
df_target = pd.read_json(s_target)
In [31]:
print(df_target)
In [32]:
df_target2 = pd.read_json(json.dumps(json.loads(s_nested)['DATA']))
In [33]:
print(df_target2)