[英]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. 我有一个带浮点数(myarray)的8 * 4 numpy数组,并希望将它转换为带有python中的熊猫的数据帧字典(并最终将其连接为一个数据帧)。 I'm coming across the error "ValueError: DataFrame constructor not properly called!" 我遇到错误“ ValueError:DataFrame构造函数未正确调用!” 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: 您无需执行所有操作,仅需使用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
Just note, you want the transpose of your array, so you can simply use myarray.T
只需注意,您需要数组的转置 ,因此您可以简单地使用myarray.T
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.