Home > Net >  Lists in dictionary using Pandas
Lists in dictionary using Pandas

Time:11-08

I am trying to create new dataframe out of the dictionary which includes lists.

It looks like something like that:

{'prices': [[1574121600000, 1.000650588888066], [1574208000000, 0.9954110116644869]...

Those are UNIX date and price of stablecoins, however the columns are not named properly as it is all under 'prices' key.

However how could I create a new df which would include 2 columns (Date, Price) using values from this dictionary?

My goal is to get something like this:

| Date             | Price |
| 15741216000000   | 1.000650588888066   |
| 15742080000000   | 0.9954110116644869   |

CodePudding user response:

You can use pd.Dataframe directly and then pass a list of column names to the parameter columns

import pandas as pd

a = {'prices': [[1574121600000, 1.000650588888066], [1574208000000, 0.9954110116644869]]}

df = pd.DataFrame(a['prices'], columns=['Date', 'Price'])
print(df)
# prints
            Date     Price
0  1574121600000  1.000651
1  1574208000000  0.995411

CodePudding user response:

d = {'prices': [[1574121600000, 1.000650588888066], [1574208000000, 0.9954110116644869]}
df = {"date":[],"prices":[]}
for k,v in d.items():
    for item in v:
        df["date"].append(item[0])
        df["prices"].append(item[1])
dataframe = pd.DataFrame(df)
  • Related