我有一个(简单的)索引问题:我有一个预分配的数组,想用计算出的值填充它以便进行进一步处理。这是一个例子:

import pandas as pd
import numpy as np

meanMov = pd.DataFrame(columns=range(3),index=range(20))

value1 = np.array([23.5,-42.34,322.4])
value2 = np.array([43,-764.56,6.4556])
value3 = np.array([98.54,-26.64,86.45])

meanMov.iloc[0:5] = value1
meanMov.iloc[5:15] = value2
meanMov.iloc[15:20] = value3


这导致以下错误:

ValueError: cannot set using a slice indexer with a different length than the value


我怎样才能解决这个问题??非常感谢!!

最佳答案

您不需要预先初始化。您只需调用带有重复值列表的repeat,然后再创建最终的DataFrame。

r = [5, 10, 5]
v = [value1, value2, value3]




pd.DataFrame(np.repeat(v, r, axis=0))

        0       1         2
0   23.50  -42.34  322.4000
1   23.50  -42.34  322.4000
2   23.50  -42.34  322.4000
3   23.50  -42.34  322.4000
4   23.50  -42.34  322.4000
5   43.00 -764.56    6.4556
6   43.00 -764.56    6.4556
7   43.00 -764.56    6.4556
8   43.00 -764.56    6.4556
9   43.00 -764.56    6.4556
10  43.00 -764.56    6.4556
11  43.00 -764.56    6.4556
12  43.00 -764.56    6.4556
13  43.00 -764.56    6.4556
14  43.00 -764.56    6.4556
15  98.54  -26.64   86.4500
16  98.54  -26.64   86.4500
17  98.54  -26.64   86.4500
18  98.54  -26.64   86.4500
19  98.54  -26.64   86.4500

10-07 22:04