简体   繁体   中英

how to create a pandas dataframe from a dictionary with column names as keys and values as rows where the values are 2-d array

I wanted to know how to create a pandas dataframe from a dictionary.

So about the dictionary d, the size of values of each key is (200, 200),

d[key].shape
(200, 200)

and there are 59 keys all together. What I want is, the keys should be the column names and for every key as column, d[key][0] can be first row of that column, d[key][1] can be the second row and so on. So in each column, each of its row will have a list of 200 as its values and in total 200 rows.

Can this be done??

Appreciate your help.

Considering d as the dictionary, with 59 keys (numerically ordered 0-58) and the value of each key is of shape (200,200).

I first created a pandas dataframe of 200 rows and 59 columns. Then later iterated through each value in the dataframe and initialised it with the d[key][row].

import pandas as pd

df = pd.DataFrame(np.zeros([200,59]),columns=d.keys(),dtype=object)

for i in range(59):
    for j in range(200):
        df[i][j] = d[i][j]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM