0
import pandas as pd

list_sample = [{'name': 'A', 'fame': 0, 'data': {'date':['2021-01-01', '2021-02-01', '2021-03-01'], 
                        'credit_score':[800, 890, 895],
                        'spend':[1500, 25000, 2400], 
                        'average_spend':5000}},
               {'name': 'B', 'fame': 1, 'data': {'date':['2022-01-01', '2022-02-01', '2022-03-01'],
                                   'credit_score':[2800, 390, 8900],
                                   'spend':[15000, 5000, 400], 
                                   'average_spend':3000}}]

df = pd.DataFrame()
for row in list_sample:
    name = row['name']
    fame = row['fame']
    data = row['data']
    df_temp = pd.DataFrame(data)
    df_temp['name'] = name
    df_temp['fame'] = fame
    df = pd.concat([df, df_temp])

Above is how I am getting my dataframe. Above is a dummy example, but, the issue with above is when the size of list grow and when the number of entries in each data array grow. Above takes alot of time. May be concat is the issue or something else, is there any better way to do what I am doing above (better in terms of run time !)

user13744439
  • 142
  • 1
  • 12

3 Answers3

1

One way of doing this is to flatten the nested data dictionary that's inside the list_sample dictionary. You can do this with json_normalize.

import pandas as pd
from pandas.io.json import json_normalize

df = pd.DataFrame(list_sample)
df = pd.concat([df.drop(['data'], axis=1), json_normalize(df['data'])], axis=1)
Mulloy
  • 156
  • 1
  • 8
0

It looks like you don't care about normalizing the data column. If that's the case, you can just do df = pd.DataFrame(list_sample) to achieve the same result. I think you'd only need to do the kind of iterating you're doing if you wanted to normalize the data.

0

Combine all dicts in list_sample to fit a dataframe structure and concat them at once:

df = pd.concat([pd.DataFrame(d['data'] | {'name': d['name'], 'fame': d['fame']}) 
                for d in list_sample])

print(df)

         date  credit_score  spend  average_spend name  fame
0  2021-01-01           800   1500           5000    A     0
1  2021-02-01           890  25000           5000    A     0
2  2021-03-01           895   2400           5000    A     0
0  2022-01-01          2800  15000           3000    B     1
1  2022-02-01           390   5000           3000    B     1
2  2022-03-01          8900    400           3000    B     1
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
  • @onyambu, perhaps, they are just envy and they are just miserable human beings. They think they are making fun, but they are just sneaking. – RomanPerekhrest Feb 15 '23 at 16:59