IF if和for循环在一行

时间:2018-03-30 12:32:46

标签: python-3.x pandas scikit-learn sklearn-pandas

我需要在单行中应用if else条件和for循环。我需要更新两个' RL'和" RM"一次又将其他值更新为'其他'。如何做???是否有可能??

train['MSZoning']=['RL' if x=='RL' else 'Others' for x in train['MSZoning']]

2 个答案:

答案 0 :(得分:4)

使用numpy.where

train['MSZoning'] = np.where(train['MSZoning'] == 'RM', 'RM', 'Others')

如果需要更新所有内容而RMRL使用isin使用~的反向布尔掩码:

train = pd.DataFrame({'MSZoning':['RL'] *3 + ['qa','RM','as']})
train.loc[~train['MSZoning'].isin(['RM','RL']), 'MSZoning'] =  'Others'

print (train)
  MSZoning
0       RL
1       RL
2       RL
3   Others
4       RM
5   Others

<强>计时

train = pd.DataFrame({'MSZoning':['RL'] *3 + ['qa','RM','as']})
#[60000 rows x 1 columns]
train = pd.concat([train] * 10000, ignore_index=True)

In [202]: %timeit train.loc[~train['MSZoning'].isin(['RM','RL']), 'MSZoning'] =  'Others'
5.82 ms ± 447 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [203]: %timeit train['MSZoning'] = train['MSZoning'].apply(lambda x: x if x in ('RM', 'RL') else 'Others')
15 ms ± 584 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

答案 1 :(得分:1)

因此,如果您希望将RMRL标记为Others,则可以使用:{/ p>

train['MSZoning'] = train['MSZoning'].apply(lambda x: x if x in ('RM', 'RL') else 'Others')