Python - How to convert JSON File to Dataframe

前端 未结 4 1369
故里飘歌
故里飘歌 2020-11-27 16:36

How can I convert a JSON File as such into a dataframe to do some transformations.

For Example if the JSON file reads:

{\"FirstName\":\"John\",

\"         


        
4条回答
  •  旧巷少年郎
    2020-11-27 16:56

    There are 2 inputs you might have and you can also convert between them.

    1. input: listOfDictionaries --> use @VikashSingh solution

    example: [{"":{"...

    The pd.DataFrame() needs a listOfDictionaries as input.

    1. input: jsonStr --> use @JustinMalinchak solution

    example: '{"":{"...

    If you have jsonStr, you need an extra step to listOfDictionaries first. This is obvious as it is generated like:

    jsonStr = json.dumps(listOfDictionaries)
    

    Thus, switch back from jsonStr to listOfDictionaries first:

    listOfDictionaries = json.loads(jsonStr)
    

提交回复
热议问题