【发布时间】:2018-11-28 04:11:11
【问题描述】:
我已经编写了一些代码,这些代码可以根据多种条件模拟 pandas 数据框中的值。我现在只想为名为@987654321@ 的列中的特定值运行此代码。我目前有以下内容:
def l_sim():
n = 100
for i in range(n)
df['RAND'] = np.random.uniform(0, 1, size=df.index.size)
conditions = [df['RAND'] >= (1 - 0.8062), (df['RAND'] < (1 - 0.8062)) & (df['RAND'] >= 0.1),
(df['RAND'] < 0.1) & (df['RAND'] >= 0.05), (df['RAND'] < 0.05) &
(df['RAND'] >= 0.025), (df['RAND'] < 0.025) & (df['RAND'] >= 0.0125),
(df['RAND'] < 0.0125)]
choices = ['L0', 'L1', 'L2', 'L3', 'L4', 'L5']
df['L'] = np.select(conditions, choices)
conditions = [df['L'] == 'L0', df['L'] == 'L1', df['L'] == 'L2', df['L'] == 'L3',
df['L'] == 'L4', df['L'] == 'L5']
choices = [df['A'] * 0.02, df['A'] * 0.15, df['A'] * 0.20, df['A'] * 0.50,
df['A'] * 1, df['A'] * 1]
df['AL'] = np.select(conditions, choices)
l_sim()
我如何才能仅对具有df.loc[df['Use Type'] == 'Commercial Property'] 的行运行此代码?
提前致谢。
【问题讨论】:
-
为什么你的代码中有一个循环
for?它似乎从未在你做的代码中使用过 -
@Ben.T 我为 range(100) 中的每个 i 获得一组不同的随机数,因此我的数据框中的每一行都有不同的“L”值。
-
好的,但是如果在每个循环中重写同一列中的“L”值,则前一个循环中的值将被删除。 AL 列也一样,你的代码也只是覆盖了这个列,不考虑前面的循环