Python spark str or bytes form dataframe conversion

Asked 2 years ago, Updated 2 years ago, 57 views

{"key1":"value1", "key2":"value2", "key3":"value3", "key4":"value4"}
{"key1":"value1", "key2":"value2", "key3":"value3" ,"key4":"value4" }
{"key1":"value1", "key2":"value2", "key3":"value3" ,"key4":"value4" }
{"key1":"value1", "key2":"value2", "key3":"value3" ,"key4":"value4" }
{"key1":"value1", "key2":"value2", "key3":"value3" ,"key4":"value4" }

When I read the content by requesting api, it is read as above.

The original is bytes, and it is also used by decoding and converting it into string How can I make this structure into a data frame?

python dataframe

2022-09-20 20:02

1 Answers

>>> import json"
>>> import pandas as df
>>> a = '{"key1":"value1", "key2":"value2", "key3":"value3" ,"key4":"value4" }'
>>> a
'{"key1":"value1", "key2":"value2", "key3":"value3" ,"key4":"value4" }'
>>> type(a)
<class 'str'>
>>> b = json.loads(a)
>>> b
{'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key4': 'value4'}
>>> type(b)
<class 'dict'>
>>> g = df.DataFrame.from_dict(b, orient='index')
>>> g
           0
key1  value1
key2  value2
key3  value3
key4  value4

Is this right?


2022-09-20 20:02

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.