[英]Appending pandas series to the left of index zero
I'm trying to select portions of a pandas data series yf
according to left limit a0
and right limit b0
.我正在尝试根据左极限a0
和右极限b0
选择熊猫数据系列yf
部分。
If the left limit is negative, I want to pad the difference with zeros so the resulting series would have the desired length, like this:如果左限制为负,我想用零填充差异,以便生成的系列具有所需的长度,如下所示:
if a0<0: ycr = pd.Series([0]*(abs(a0))).append(yf[:b0])
but this is returning:但这又回来了:
Series([], Name: 1, dtype: float64)
and no more information is given.并且没有提供更多信息。
I created the source Series as:我将源系列创建为:
lst = np.arange(10,20)
yf = pd.Series(lst + 5, index = lst)
so that it contains:以便它包含:
10 15
11 16
12 17
13 18
14 19
15 20
16 21
17 22
18 23
19 24
dtype: int32
(the left column is the index, and the right - actual values). (左列是索引,右列是实际值)。
Then, to create an output Series composed of 3 zeroes and then 5 initial elements of yf I ran:然后,要创建一个由 3 个零和yf 的5 个初始元素组成的输出系列,我运行了:
a0 = -3; b0 = 5
ycr = pd.Series([0]*(abs(a0))).append(yf[:b0])
and got:并得到:
0 0
1 0
2 0
10 15
11 16
12 17
13 18
14 19
dtype: int64
Then I performed another test, on the source Series created with the default index (consecutive integers from 0 ):然后我对使用默认索引(从0 开始的连续整数)创建的源系列进行了另一个测试:
yf = pd.Series(lst + 5)
This time the result is:这次的结果是:
0 0
1 0
2 0
0 15
1 16
2 17
3 18
4 19
dtype: int64
(the only difference is in the index column, as I expected). (唯一的区别在于索引列,正如我所料)。
So, as you can see, your code works as expected.因此,如您所见,您的代码按预期工作。 Probably there is something wrong with your source data.您的源数据可能有问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.