0

我想将 600-1200 范围内的随机数值分配给我的列df['size'],但基于另一列。因此,如果同一行的df['type']值为 yes,则 的值df['size']应在 600-1200 的范围内,但如果 的值为df['type']“否”,则 的值df['size']应为 1500-2000 之间的随机值。

df['size'] = np.random.randint(600,1200, size=len(df))
4

1 回答 1

2

用于numpy.where选择是否匹配yes

a = np.random.randint(600,1200, size=len(df))
b = np.random.randint(1500,2000, size=len(df))
df['size'] = np.where(df['type'].eq('yes'), a, b)
于 2021-03-15T14:48:47.523 回答