简体   繁体   中英

np.array to dictionary of dataframes: ValueError: DataFrame constructor not properly called

I have an 8*4 numpy array with floats (myarray) and would like to transform it into a dictionary of dataframes (and eventually concatenate it into one dataframe) with pandas in python. I'm coming across the error "ValueError: DataFrame constructor not properly called!" though. Here is the way I attempt it:

    mydict={}
    for i, y in enumerate(np.arange(2015,2055,5)):
      for j, s in enumerate(['Winter', 'Spring', 'Summer', 'Fall']):
        mydict[(y,s)]=pd.DataFrame(myarray[i,j])
    mydict

Any ideas? Thanks!

As requested, some sample data:

array([[ 29064908.33333333,  33971366.66666667,  37603508.33333331,
     37105916.66666667],
   [ 25424991.66666666,  30156625.        ,  32103324.99999999,
     31705075.        ],
   [ 26972666.66666666,  28182699.99999995,  30614324.99999999,
     29673008.33333333],
   [ 26923466.66666666,  27573075.        ,  28308725.        ,
     27834291.66666666],
   [ 26015216.66666666,  28709191.66666666,  30807833.33333334,
     27183991.66666684],
   [ 25711475.        ,  32861633.33333332,  35784916.66666666,
     28748891.66666666],
   [ 26267299.99999999,  35030583.33333331,  37863808.33333329,
     29931858.33333332],
   [ 28871674.99999998,  38477549.99999999,  40171374.99999999,
     33853750.        ]])

and expected output:

            2015    2020    2025    2030    2035    2040    2045    2050
    Winter  2.9e+07 2.5e+07 2.6e+07 2.6e+07 2.6e+07 2.5e+07 2.6e+07 2.8e+07
    Spring  3.3e+07 3.0e+07 2.8e+07 2.7e+07 2.8e+07 3.2e+07 3.5e+07 3.8e+07
    Summer  3.7e+07 3.2e+07 3.0e+07 2.8e+07 3.0e+07 3.5e+07 3.7e+07 4.0e+07
    Fall    3.7e+07 3.1e+07 2.9e+07 2.7e+07 2.7e+07 2.8e+07 2.9e+07 3.3e+07
mydict = {}

myarray = np.random.rand(8, 4)

for i, y in enumerate(range(2015, 2055, 5)):
    for j, s in enumerate(['Winter', 'Spring', 'Summer', 'Fall']):
        mydict[str(y) + ' ' + s] = myarray[i, j]

df = pd.DataFrame(mydict, index = [0]).transpose()
df.columns = ['Measure']

df

I don't fully understand the indexing used here.

You don't need to do all that, just use the DataFrame constructor - that's what it's for:

In [10]: idx = ['Winter', 'Spring', 'Summer', 'Fall']

In [11]: cols = np.arange(2015,2055,5)

In [12]: pd.DataFrame(myarray.T, index=idx, columns=cols)
Out[12]:
                2015          2020          2025          2030          2035  \
Winter  2.906491e+07  2.542499e+07  2.697267e+07  2.692347e+07  2.601522e+07
Spring  3.397137e+07  3.015662e+07  2.818270e+07  2.757308e+07  2.870919e+07
Summer  3.760351e+07  3.210332e+07  3.061432e+07  2.830872e+07  3.080783e+07
Fall    3.710592e+07  3.170508e+07  2.967301e+07  2.783429e+07  2.718399e+07

                2040          2045        2050
Winter  2.571148e+07  2.626730e+07  28871675.0
Spring  3.286163e+07  3.503058e+07  38477550.0
Summer  3.578492e+07  3.786381e+07  40171375.0
Fall    2.874889e+07  2.993186e+07  33853750.0

Just note, you want the transpose of your array, so you can simply use myarray.T

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