Python-熊猫使用np.where创建多个不同值的列

时间:2018-07-11 02:52:58

标签: python pandas numpy

我有一个这样的数据框:

id year Q
1 2017 1
2 2017 2
3 2018 1
4 2018 2
5 2018 3

并希望创建另一个名为D的列:

id year Q Desc
1 2017 1 '2017 first Quarter'
2 2017 2 '2017 second Quarter'
3 2018 1 '2018 first Quarter' 
4 2018 2 '2018 second Quarter' 
5 2018 3 '2017 third Quarter'

但是np.where似乎只接受2个参数(如果为true和false)。因此,我们的想法是拥有一个可以处理多种可能性的np.where句子

3 个答案:

答案 0 :(得分:2)

大致上,使用np.select

df['Desc'] = df.year.astype(str) + ' ' + np.select([df.Q==x for x in [1,2,3,4]], ['first', 'second', 'third', 'quarter']) + ' quarter'

答案 1 :(得分:2)

IIUC,您只需要将Q列map移至character,然后使用字符串总和

df['Desc']=df.year.astype(str)+' '+ df.Q.map({1:'first',2:'second',3:'third',4:'fourth'})+' Quarter'
df
Out[26]: 
   id  year  Q                 Desc
0   1  2017  1   2017 first Quarter
1   2  2017  2  2017 second Quarter
2   3  2018  1   2018 first Quarter
3   4  2018  2  2018 second Quarter
4   5  2018  3   2018 third Quarter

答案 2 :(得分:0)

使用+pandas.DataFrame.apply

d={1:'first',2:'second',3:'third',4:'fourth'}
df['Desc']=(df['year'].astype(str)+' '+df['Q'].astype(str)+' Quarter').apply(lambda x: ' '.join(d[int(i)] if len(i)==1 else i for i in x.split()))
print(df)

输出:

   id  year  Q                 Desc
0   1  2017  1   2017 first Quarter
1   2  2017  2  2017 second Quarter
2   3  2018  1   2018 first Quarter
3   4  2018  2  2018 second Quarter
4   5  2018  3   2018 third Quarter