根据特定条件分配新列

时间:2018-09-09 23:46:04

标签: python pandas

我有一个数据框,我需要一种对它应用不同条件的方法。如果点<20,则very low 点> 20且<80则low 点> 80并且小于<= 150 medium 点> 150,然后high

     Points
0     10
1     08
2     25
3     40
4     90
5    150
6    350

所需的输出:

     Points     new
0     10     very low
1     08     very low
2     25          low
3     40          low
4     90       medium
5    150       medium
6    350         High

我能够用一个函数和很少的条件来解决它,但是我正在寻找一种更好的熊猫方法。

2 个答案:

答案 0 :(得分:3)

您可以使用cut

pd.cut(df.Points,bins=[0,20,80,150,999],labels=['very low','low','medium','High'])
Out[64]: 
0    very low
1    very low
2         low
3         low
4      medium
5      medium
6        High
Name: Points, dtype: category
#df['new']=pd.cut(df.Points,bins=[0,20,80,150,999],labels=['very low','low','medium','High'])

答案 1 :(得分:2)

使用import numpy as np conds = [df.Points < 20, df.Points < 80, df.Points <=150] choices = ['very low', 'low', 'medium'] df['new'] = np.select(conds, choices, default = 'high') >>> df Points new 0 10 very low 1 8 very low 2 25 low 3 40 low 4 90 medium 5 150 medium 6 350 high

numpy

注意:如果要避免显式导入pd.np,可以将df['new'] = pd.np.select(conds, choices, default = 'high') 用作包装器:

{{1}}