【发布时间】: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