Python:使用 np.where 维护多列

MaM*_*aMo 5 python dataframe pandas

是否可以使用同时维护多个列np.where?通常,一列是用 维护的np.where,所以我的编码如下所示:

df['col1'] = np.where(df[df.condition == 'yes'],'sth', '')
df['col2'] = np.where(df[df.condition == 'yes'], 50.00, 0.0)
Run Code Online (Sandbox Code Playgroud)

但由于我对相同的条件进行了两次测试,我想知道是否可以通过 2 列并在一次运行中填充它们。

我试过这个:

df['col1','col2'] = np.where(df[df.condition == 'yes'],['sth',50.00], ['',0.0])
Run Code Online (Sandbox Code Playgroud)

但这不起作用。有没有办法实现这一点?

jez*_*ael 2

我认为需要将布尔掩码重塑为(N x 1)

m = df.condition == 'yes'
df[['col1','col2']] = pd.DataFrame(np.where(m[:, None], ['sth',50.00], ['',0.0]))
Run Code Online (Sandbox Code Playgroud)

该解决方案的唯一缺点是,如果 s 中的值类型不同list- 数字与strings - 那么numpy.where两个输出列都会转换为strings。

样本

df = pd.DataFrame({'A':list('abcdef'),
                     'condition':['yes'] * 3 + ['no'] * 3})

print (df)
   A condition
0  a       yes
1  b       yes
2  c       yes
3  d        no
4  e        no
5  f        no

m = df.condition == 'yes'
df[['col1','col2']] = pd.DataFrame(np.where(m[:, None], ['sth',50.00], ['',0.0]))
print (df)
   A condition col1  col2
0  a       yes  sth  50.0
1  b       yes  sth  50.0
2  c       yes  sth  50.0
3  d        no        0.0
4  e        no        0.0
5  f        no        0.0

print (df.applymap(type))
               A      condition           col1           col2
0  <class 'str'>  <class 'str'>  <class 'str'>  <class 'str'>
1  <class 'str'>  <class 'str'>  <class 'str'>  <class 'str'>
2  <class 'str'>  <class 'str'>  <class 'str'>  <class 'str'>
3  <class 'str'>  <class 'str'>  <class 'str'>  <class 'str'>
4  <class 'str'>  <class 'str'>  <class 'str'>  <class 'str'>
5  <class 'str'>  <class 'str'>  <class 'str'>  <class 'str'>
Run Code Online (Sandbox Code Playgroud)

编辑:我用NaNs 值测试它:

df = pd.DataFrame({'A':list('abcdefghi'),
                     'condition':['yes'] * 3 + ['no'] * 3 + [np.nan] * 3})

m = df.condition == 'yes'
df[['col1','col2']] = pd.DataFrame(np.where(m[:, None], ['sth',50.00], ['',0.0]))
print (df)
   A condition col1  col2
0  a       yes  sth  50.0
1  b       yes  sth  50.0
2  c       yes  sth  50.0
3  d        no        0.0
4  e        no        0.0
5  f        no        0.0
6  g       NaN        0.0
7  h       NaN        0.0
8  i       NaN        0.0
Run Code Online (Sandbox Code Playgroud)