Convert JSON file to Pandas dataframe

后端 未结 4 1493
醉话见心
醉话见心 2021-02-09 23:40

I would like to convert a JSON to Pandas dataframe.

My JSON looks like: like:

{ 
   \"country1\":{ 
      \"AdUnit1\":{ 
         \"floor_price1\":{ 
            


        
4条回答
  •  被撕碎了的回忆
    2021-02-09 23:57

    You could use this:

    def flatten_dict(d):
        """ Returns list of lists from given dictionary """
        l = []
        for k, v in sorted(d.items()):
            if isinstance(v, dict):
                flatten_v = flatten_dict(v)
                for my_l in reversed(flatten_v):
                    my_l.insert(0, k)
    
                l.extend(flatten_v)
    
            elif isinstance(v, list):
                for l_val in v:
                    l.append([k, l_val])
    
            else:
                l.append([k, v])
    
        return l
    

    This function receives a dictionary (including nesting where values could also be lists) and flattens it to a list of lists.

    Then, you can simply:

    df = pd.DataFrame(flatten_dict(my_dict))
    

    Where my_dict is your JSON object. Taking your example, what you get when you run print(df) is:

              0        1             2         3     4
    0  country1  AdUnit1  floor_price1  feature1  1111
    1  country1  AdUnit1  floor_price1  feature2  1112
    2  country1  AdUnit1  floor_price2  feature1  1121
    3  country1  AdUnit2  floor_price1  feature1  1211
    4  country1  AdUnit2  floor_price2  feature1  1221
    5  country2  AdUnit1  floor_price1  feature1  2111
    6  country2  AdUnit1  floor_price1  feature2  2112
    

    And when you create the dataframe, you can name your columns and index

提交回复
热议问题