I need to convert a JSON string with 2 columns and an index into a pandas dataframe but I'm facing an error I can't find a solution.
I have tried to encode the JSON with different orientations orient='columns
and orient='index
, I also normalized the JSON string and specified the columns name but there is still an error. It looks like it's a bit tricky, how can I do that?
This is the string I use:
> type(data)
<class 'str'>
> print(data)
{
"2020-04-02T00:00:00.000Z": {
"A": 133.25,
"B": 0.000155642
},
"2020-04-03T00:00:00.000Z": {
"A": 136.45,
"B": 0.0001498913
},
"2020-04-04T00:00:00.000Z": {
"A": 141.55,
"B": 0.0001471562
}
}
df = pd.DataFrame.from_dict(data)
df = pd.DataFrame.from_dict(json_normalize(data), orient='index', columns=['A', 'B'])
Python throws an error saying AttributeError: 'str' object has no attribute 'values'
.
What I would like is a dataframe like this:
A B
2020-04-02 00:00:00+00:00 133.25 0.000156
2020-04-03 00:00:00+00:00 136.45 0.000150
2020-04-04 00:00:00+00:00 141.55 0.000147
Thank you,
You can use pandas.read_json
. From the doc you can specify orient
= index
which is an Indication of expected JSON string format in dict like {index -> {column -> value}}
>>> import pandas as pd
>>> data = """
... {
... "2020-04-02T00:00:00.000Z": {
... "A": 133.25,
... "B": 0.000155642
... },
... "2020-04-03T00:00:00.000Z": {
... "A": 136.45,
... "B": 0.0001498913
... },
... "2020-04-04T00:00:00.000Z": {
... "A": 141.55,
... "B": 0.0001471562
... }
... }"""
>>>
>>> df = pd.read_json(data, orient='index')
>>> df
A B
2020-04-02 00:00:00+00:00 133.25 0.000156
2020-04-03 00:00:00+00:00 136.45 0.000150
2020-04-04 00:00:00+00:00 141.55 0.000147
You can use T like this:
df = pd.DataFrame.from_dict(data).T
and result is this:
A B
2020-04-02T00:00:00.000Z 133.25 0.000156
2020-04-03T00:00:00.000Z 136.45 0.000150
2020-04-04T00:00:00.000Z 141.55 0.000147
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.