我的数据框如下所示:
|userid|rank2017|rank2018|
|212 |'H' |'H' |
|322 |'L' |'H |
|311 |'H' |'L' |
我想在上面的数据框中创建一个名为progress的新列,如果rank2017等于rank2018,则输出1;如果rank2017为'H',则输出2,并且rank2018为'L',否则3.可以任何人帮我执行此在python中
答案 0 :(得分:2)
以下是使用np.select
的方法:
# Set your conditions:
conds = [(df['rank2017'] == df['rank2018']),
(df['rank2017'] == 'H') & (df['rank2018'] == 'L')]
# Set the values for each conditions
choices = [1, 2]
# Use np.select with a default of 3 (your "else" value)
df['progress'] = np.select(conds, choices, default = 3)
返回:
>>> df
userid rank2017 rank2018 progress
0 212 H H 1
1 322 L H 3
2 311 H L 2
答案 1 :(得分:1)
这是一种方法。您不需要使用嵌套的if语句。
df = pd.DataFrame({'user': [212, 322, 311],
'rank2017': ['H', 'L', 'H'],
'rank2018': ['H', 'H', 'L']})
df['progress'] = 3
df.loc[(df['rank2017'] == 'L') & (df['rank2018'] == 'H'), 'progress'] = 2
df.loc[df['rank2017'] == df['rank2018'], 'progress'] = 1
# rank2017 rank2018 user progress
# 0 H H 212 1
# 1 L H 322 2
# 2 H L 311 3