Python 基于多个条件填充新列

Python 基于多个条件填充新列,python,pandas,conditional-statements,criteria,Python,Pandas,Conditional Statements,Criteria,假设我有一个包含三个分类列的数据集:df.type1 df.type2 df.type3,我想创建一个新列[df.new],它需要: df.new = df.type1 if df.type1 is true and the remaining are false df.new = df.type2 if df.type2 is true and the remaining are false df.new = df.type3 if df.type3 is true and the remai

假设我有一个包含三个分类列的数据集:df.type1 df.type2 df.type3,我想创建一个新列[df.new],它需要:

df.new = df.type1 if df.type1 is true and the remaining are false
df.new = df.type2 if df.type2 is true and the remaining are false
df.new = df.type3 if df.type3 is true and the remaining are false
最好的方法是什么?我对np.where()感到非常困惑-太长太激烈

例如:

City    dt.t1   dt.t2   dt.t3
NY       US Non    EU   Non Asia
Rome     Non US    EU   Non Asia
SF       US Non    EU   Non Asia
HK       Non US    Non EU   Asia
我的最终结果是:

City  dt.new
NY    US
Rome  EU
SF    US
HK    Asia
使用:

选择列作为检查值的备选解决方案:

cols = df.filter(regex='^dt\.').columns
#or use list of columns names
#cols = ['dt.t1','dt.t2','dt.t3']
df['dt.new'] = df[cols].mask(df[cols].apply(lambda x: x.str.contains('Non\s+'))).ffill(axis=1).iloc[:, -1]

详细信息

首先按
City
列,然后检查带有一个或多个空格的
Non
字符串:

df = df.set_index('City')

print (df.apply(lambda x: x.str.contains('Non\s+')))
      dt.t1  dt.t2  dt.t3
City                     
NY    False   True   True
Rome   True  False   True
SF    False   True   True
HK     True   True  False
然后将匹配的值替换为
NaN
s,方法是:

每行正向填充非缺失值:

print (df.mask(df.apply(lambda x: x.str.contains('Non\s+'))).ffill(axis=1))
     dt.t1 dt.t2 dt.t3
City                  
NY      US    US    US
Rome   NaN    EU    EU
SF      US    US    US
HK     NaN   NaN  Asia
最后选择最后一列:

print (df.mask(df.apply(lambda x: x.str.contains('Non\s+'))).ffill(axis=1).iloc[:, -1])
City
NY        US
Rome      EU
SF        US
HK      Asia
Name: dt.t3, dtype: object
编辑:

或:



是否可以添加小数据样本?感谢您提供的数据,预期的输出是什么?谢谢,在我的具体案例中,df.t1 df.t2 dt.3是对象:df.t1=['EU'、'Non-EU'],df.t2=['US'、'Non-US']df.t3=[['Asia'、'Non-Asia'],如何让它在这种情况下工作?@FilippoSebastio-什么是条件?非常感谢,但我发现这个脚本比np.where()…yopu如何使用np.where()?似乎我无法获得它right@FilippoSebastio-当然,妈妈。
print (df.mask(df.apply(lambda x: x.str.contains('Non\s+'))))
     dt.t1 dt.t2 dt.t3
City                  
NY      US   NaN   NaN
Rome   NaN    EU   NaN
SF      US   NaN   NaN
HK     NaN   NaN  Asia
print (df.mask(df.apply(lambda x: x.str.contains('Non\s+'))).ffill(axis=1))
     dt.t1 dt.t2 dt.t3
City                  
NY      US    US    US
Rome   NaN    EU    EU
SF      US    US    US
HK     NaN   NaN  Asia
print (df.mask(df.apply(lambda x: x.str.contains('Non\s+'))).ffill(axis=1).iloc[:, -1])
City
NY        US
Rome      EU
SF        US
HK      Asia
Name: dt.t3, dtype: object
m1 = df['dt.t1'] == 'US'
m2 = df['dt.t2'] == 'EU'
m3 = df['dt.t3'] == 'Asia'

df['dt.new'] = np.select([m1, m2, m3], ['US','EU','Asia'], default=None)
df['dt.new'] = np.where(m1, 'US',
               np.where(m2, 'EU',
               np.where(m3, 'Asia', None)))
print (df)
   City   dt.t1   dt.t2     dt.t3 dt.new
0    NY      US  Non EU  Non Asia     US
1  Rome  Non US      EU  Non Asia     EU
2    SF      US  Non EU  Non Asia     US
3    HK  Non US  Non EU      Asia   Asia