gpt4 book ai didi

python - 在条件下向前填充列

转载 作者:行者123 更新时间:2023-12-03 15:27:41 25 4
gpt4 key购买 nike

关闭。这个问题需要更多focused .它目前不接受答案。












想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post .

去年关闭。




Improve this question




我的数据框看起来像这样;

df = pd.DataFrame({'Col1':[0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0]
,'Col2':[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]})

如果 col1 在第 2 列中包含值 1,我想用 1 n 次向前填充。例如,如果 n = 4 那么我需要结果看起来像这样。
df = pd.DataFrame({'Col1':[0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0]
,'Col2':[0,1,1,1,1,0,0,0,1,1,1,1,0,0,0,0,0,1,1,1,1]})

我想我可以使用带有计数器的 for 循环来做到这一点,该计数器在每次出现条件时都会重置,但是有没有更快的方法来产生相同的结果?

谢谢!

最佳答案

方法#1:一个基于 NumPy 的 1D convolution ——

N = 4 # window size
K = np.ones(N,dtype=bool)
df['Col2'] = (np.convolve(df.Col1,K)[:-N+1]>0).view('i1')

更紧凑的单衬 -
df['Col2'] = (np.convolve(df.Col1,[1]*N)[:-N+1]>0).view('i1')

方法#2:这是一个 SciPy's binary_dilation ——
from scipy.ndimage.morphology import binary_dilation

N = 4 # window size
K = np.ones(N,dtype=bool)
df['Col2'] = binary_dilation(df.Col1,K,origin=-(N//2)).view('i1')

方法#3:使用基于跨 View 的工具从 NumPy 中挤出最好的 -
from skimage.util.shape import view_as_windows

N = 4 # window size
mask = df.Col1.values==1
w = view_as_windows(mask,N)
idx = len(df)-(N-mask[-N:].argmax())
if mask[-N:].any():
mask[idx:idx+N-1] = 1
w[mask[:-N+1]] = 1
df['Col2'] = mask.view('i1')

基准测试

给定样本的设置按 10,000x 放大——
In [67]: df = pd.DataFrame({'Col1':[0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0]
...: ,'Col2':[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]})
...:
...: df = pd.concat([df]*10000)
...: df.index = range(len(df.index))

计时
# @jezrael's soln
In [68]: %%timeit
...: n = 3
...: df['Col2_1'] = df['Col1'].where(df['Col1'].eq(1)).ffill(limit=n).fillna(df['Col1']).astype(int)
5.15 ms ± 25.3 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

# App-1 from this post
In [72]: %%timeit
...: N = 4 # window size
...: K = np.ones(N,dtype=bool)
...: df['Col2_2'] = (np.convolve(df.Col1,K)[:-N+1]>0).view('i1')
1.41 ms ± 20.9 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

# App-2 from this post
In [70]: %%timeit
...: N = 4 # window size
...: K = np.ones(N,dtype=bool)
...: df['Col2_3'] = binary_dilation(df.Col1,K,origin=-(N//2)).view('i1')
2.92 ms ± 13.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

# App-3 from this post
In [35]: %%timeit
...: N = 4 # window size
...: mask = df.Col1.values==1
...: w = view_as_windows(mask,N)
...: idx = len(df)-(N-mask[-N:].argmax())
...: if mask[-N:].any():
...: mask[idx:idx+N-1] = 1
...: w[mask[:-N+1]] = 1
...: df['Col2_4'] = mask.view('i1')
1.22 ms ± 3.02 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

# @yatu's soln
In [71]: %%timeit
...: n = 4
...: ix = (np.flatnonzero(df.Col1 == 1) + np.arange(n)[:,None]).ravel('F')
...: df.loc[ix, 'Col2_5'] = 1
7.55 ms ± 32 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

关于python - 在条件下向前填充列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60801543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com