2 回答
TA贡献1951条经验 获得超3个赞
你可以使用系列面具和 loc
df['P'] = float('nan')
df['P'].loc[(df['On'] >= 1) & (df['On'] <= 3)] = 1
df['P'].loc[(df['On'] >= 4) & (df['On'] <= 6)] = 2
# ...etc
用循环扩展它很容易
j = 1
for i in range(1, 20):
df['P'].loc[(df['On'] >= j) & (df['On'] <= (j+2))] = i
j += 3
TA贡献1824条经验 获得超5个赞
通过一些基本的数学和矢量化,您可以获得更好的性能。
import pandas as pd
import numpy as np
n = 1000
df = pd.DataFrame({"On":np.random.randint(1,20, n)})
AlexG的解决方案
%%time
j = 1
df["P"] = np.nan
for i in range(1, 20):
df['P'].loc[(df['On'] >= j) & (df['On'] <= (j+2))] = i
j += 3
CPU times: user 2.11 s, sys: 0 ns, total: 2.11 s
Wall time: 2.11 s
建议的解决方案
%%time
df["P"] = np.ceil(df["On"]/3)
CPU times: user 2.48 ms, sys: 0 ns, total: 2.48 ms
Wall time: 2.15 ms
加速是 ~1000 倍
添加回答
举报