Home > Software engineering >  Convert tuple and nested dictionary to DataFrame
Convert tuple and nested dictionary to DataFrame

Time:01-24

I have the following code in my function get_context_data(self, **kwargs):

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['uploaded'] = self.request.session['uploaded']
        context['selection'] = self.request.session['selection']
        dict_comparaison_p1 = dict(enumerate(context['uploaded'].items()))
        list_comparaison_p2 = context['selection']
        dict_comparaison_p2 = {number: dict_comparaison_p1[number] for number in list_comparaison_p2}
        pprint(dict_comparaison_p2)

My dict_comparaison_p2 has the following output:

{0: ('Product', {'0': 'Smartwatch', '1': 'Shoes', '2': 'Necklace'}),
 1: (' Weight', {'0': 2, '1': 50, '2': 40}),
 2: (' Price', {'0': 100, '1': 240, '2': 230})}

I want to convert it to a DataFrame like that:

      Product   Weight   Price
0  Smartwatch        2     100
1       Shoes       50     240
2    Necklace       40     230

But when I try to convert it with my output, I have the following result:

                                                   0                           1                               2
0                                            Product                      Weight                           Price
1  {'0': 'Smartwatch', '1': 'Shoes', '2': 'Neckla...  {'0': 2, '1': 50, '2': 40}  {'0': 100, '1': 240, '2': 230}

Could you please help me with that?

Thanks!

CodePudding user response:

One possible way to do it:

import pandas as pd

d = {0: ('Product', {'0': 'Smartwatch', '1': 'Shoes', '2': 'Necklace'}),
     1: (' Weight', {'0': 2, '1': 50, '2': 40}),
     2: (' Price', {'0': 100, '1': 240, '2': 230})}

d_col = {}
df = pd.DataFrame()
idx = 0
for k,v in d.items():
    d_col[k] = v[0]
    df = df.append(pd.DataFrame(data=v[1],index=[idx]))
    idx =1

df = df.T.rename(columns = d_col)
print(df)

I break the tuple up and manipulate the information as needed. First, to make a dictionary called d_col. I append the data we want to an initially empty DataFrame and keep appending. Later, I use the dictionary d_col in order to label the df.T with the correct column names based on index, as I had set these indices based on idx earlier in the loop.

Output:

      Product  Weight  Price
0  Smartwatch       2    100
1       Shoes      50    240
2    Necklace      40    230
  •  Tags:  
  • Related