【问题标题】:np.array to dictionary of dataframes: ValueError: DataFrame constructor not properly callednp.array 到数据框字典:ValueError:DataFrame 构造函数未正确调用
【发布时间】:2018-05-06 14:05:53
【问题描述】:

我有一个带有浮点数 (myarray) 的 8*4 numpy 数组,并希望在 python 中使用 pandas 将其转换为数据帧字典(并最终将其连接成一个数据帧)。我遇到了错误“ValueError:DataFrame 构造函数没有正确调用!”尽管。这是我尝试的方式:

    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

有什么想法吗?谢谢!

根据要求,一些示例数据:

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.        ]])

和预期的输出:

            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

【问题讨论】:

  • 您介意显示一些示例数据以及预期输出吗?这可能不是做你想做的事的好方法。看看提供一个minimal reproducible example

标签: python pandas numpy dictionary dataframe


【解决方案1】:

您不需要做所有这些,只需使用 DataFrame 构造函数 - 这就是它的用途:

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

请注意,您需要数组的转置,因此您可以简单地使用myarray.T

【讨论】:

  • 好吧,这很公平,要容易得多..!只是为了解释我为什么这样做:通常我使用更大的维度(不是二维),这意味着 DataFrame 构造函数不接受我的 n 维数组(n>2)。所以为了解决这个问题,我做了我最初发布的事情:我制作了一个数据框字典,然后将它们连接起来。那么是否还有更好的方法来做到这一点?
【解决方案2】:
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

我不完全理解这里使用的索引。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-01
    • 2021-11-27
    • 2019-02-27
    • 2018-08-27
    • 1970-01-01
    • 1970-01-01
    • 2017-07-27
    • 1970-01-01
    相关资源
    最近更新 更多