小编jar*_*arm的帖子

了解列表切片中的负面步骤

我试图了解以下行为,并欢迎任何参考(尤其是官方文档)或评论。

让我们考虑一个列表:

>>> x = [1,2,3,4,5,6]
Run Code Online (Sandbox Code Playgroud)

这按预期工作

>>> x[-1:-4:-1] 
[6, 5, 4]
Run Code Online (Sandbox Code Playgroud)

但我很惊讶以下是空的:

>>>  x[0:-4:-1] 
[]
Run Code Online (Sandbox Code Playgroud)

因此,我很惊讶以下不是空的

>>> x[0:-len(x)-1:-1]
> [1]
Run Code Online (Sandbox Code Playgroud)

特别是考虑到

>>> x[0:-len(x):-1] 
[]
Run Code Online (Sandbox Code Playgroud)

还有那个

>>> x[0:-len(x)-1] 
[]
Run Code Online (Sandbox Code Playgroud)

是空的。

python list slice negative-number

5
推荐指数
2
解决办法
3126
查看次数

比 itertuples 内的 loc 更快地更新列的块

我有以下代码,它遍历数据帧并根据其他两个列更新列的块。当前的解决方案使用locinside itertuples.

是否可以在不诉诸手动并行化或拆分数据帧的情况下使代码更快?

n_rows = 10000
ix_ = pd.date_range(start="2020-01-01 00:00", freq="min", periods=n_rows)
offsets_ = pd.to_timedelta(np.random.randint(0, 60, size=n_rows), unit="min")
df = pd.DataFrame(
    ix_ + pd.to_timedelta(offsets_, unit="min"), index=ix_, columns=["t_end"]
)
df["active"] = 0
for row in df.itertuples():
    df.loc[row.Index : row.t_end, "active"] += 1
Run Code Online (Sandbox Code Playgroud)

dataframe pandas

5
推荐指数
1
解决办法
135
查看次数

标签 统计

dataframe ×1

list ×1

negative-number ×1

pandas ×1

python ×1

slice ×1