pandas 如何通过使用where、select、loc检查不同列上的条件来为一列赋值

js5cn81o  于 2023-02-14  发布在  其他
关注(0)|答案(2)|浏览(157)

根据 Dataframe 中的多列为列赋值
我有以下代码-在那里我试图赋值给一个列的人的年龄为基础

conditions = [df['age']<=25,df['age']>25,df['age']>=50]
values = ['age below 25','between 25 and 50','50+']
df['age category']=np.select(conditions,values)

输出-

gender  name    age age category
0   male    A   45  between 25 and 50
1   female  B   22  age below 25
2   other   C   54  between 25 and 50

对于54岁,应将年龄类别指定为50岁以上
所以我已经尝试了下面的代码,它显示了一个错误

conditions = [df['age']<=25,(df['age']>25 & df['age']<50),df['age']>=50]
values = ['age below 25','between 25 and 50','50+']
df['age category']=np.select(conditions,values)

我想我们可以使用where,select或者loc来实现这个,但是完全不确定..提前感谢

n9vozmp4

n9vozmp41#

我在这里使用cut

### user defined threshold ages in order
ages = [25, 50]

### below is programmatic
labels = ([f'age below {ages[0]}']
          +[f'between {a} and {b}'
            for a,b in zip(ages, ages[1:])]
          +[f'{ages[-1]}+']
         )

df['age category'] = pd.cut(df['age'], bins=[0]+ages+[np.inf], labels=labels)

输出:

gender name  age       age category
0    male    A   45  between 25 and 50
1  female    B   22       age below 25
2   other    C   54                50+
irlmq6kh

irlmq6kh2#

您可以使用defaultnp.select参数,由于遇到的第一个条件已选定,因此您可以用途:

conditions = [df['age'] < 25, df['age'] < 50]
values = ['age below 25', 'between 25 and 50']
df['age category'] = np.select(conditions, values, default='50+')
print(df)

# Output:
    age       age category
0    56                50+
1    18       age below 25
2    39  between 25 and 50
3    21       age below 25
4    13       age below 25
5    24       age below 25
6    54                50+
7    47  between 25 and 50
8    43  between 25 and 50
9    60                50+
10   65                50+
11   21       age below 25
12   53                50+
13   66                50+
14   52                50+
15   13       age below 25
16   10       age below 25
17   46  between 25 and 50
18   13       age below 25
19   57                50+

相关问题