Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

convert list of lists to pandas data frame

I have a list of lists, a sample of which is pasted below. I would like to convert this to a pandas data frame but the list contains many duplicates. How would I remove duplicates from a list of lists like this and convert to a data frame with two columns: timestamp and price?

[[{'timestamp': 1648558320942, 'price': 47876.0},
  {'timestamp': 1648558320942, 'price': 47876.0}],
 [{'timestamp': 1648558321945, 'price': 47881.0},
  {'timestamp': 1648558321945, 'price': 47881.0},
  {'timestamp': 1648558321945, 'price': 47881.0}],
 [{'timestamp': 1648558326768, 'price': 47876.0}]]

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can flatten the list and drop duplicates from your dataframe.

# import toolboxes
import pandas as pd
from itertools import chain

# get data
data = [[{'timestamp': 1648558320942, 'price': 47876.0},
      {'timestamp': 1648558320942, 'price': 47876.0}],
     [{'timestamp': 1648558321945, 'price': 47881.0},
      {'timestamp': 1648558321945, 'price': 47881.0},
      {'timestamp': 1648558321945, 'price': 47881.0}],
     [{'timestamp': 1648558326768, 'price': 47876.0}]]

# flatten, create df and drop duplicates
a = list(chain.from_iterable(data))
df = pd.DataFrame(a)
df = df.drop_duplicates()

Output:

print(df)
       timestamp    price
0  1648558320942  47876.0
2  1648558321945  47881.0
5  1648558326768  47876.0
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading