Python 选择带条件的列数据并将其移动到新列

Python 选择带条件的列数据并将其移动到新列,python,pandas,Python,Pandas,我有一个如下所示的数据框 T$QOOR 3 14 12 -6 -19 9 我想把正数和负数移到新的列中 sls_item['SALES'] = sls_item['T$QOOR'].apply(lambda x: x if x >= 0 else 0) sls_item['RETURN'] = sls_item['T$QOOR'].apply(lambda x: x*-1 if x < 0 else 0) 除了使用应用?溶液和 ,也可通过-1添加多个:

我有一个如下所示的数据框

T$QOOR
   3
  14
  12
  -6
 -19
   9
我想把正数和负数移到新的列中

sls_item['SALES'] = sls_item['T$QOOR'].apply(lambda x: x if x >= 0 else 0)
sls_item['RETURN'] = sls_item['T$QOOR'].apply(lambda x: x*-1 if x < 0 else 0)
除了使用
应用

溶液和 ,也可通过
-1添加多个:

sls_item['SALES'] = sls_item['T$QOOR'].clip_lower(0)
sls_item['RETURN'] = sls_item['T$QOOR'].clip_upper(0).mul(-1)
print (sls_item)
   T$QOOR  SALES  RETURN
0       3      3       0
1      14     14       0
2      12     12       0
3      -6      0       6
4     -19      0      19
5       9      9       0
使用或:


关联
+
其中

df.assign(po=df.where(df['T$QOOR']>0,0),ne=df.where(df['T$QOOR']<0,0))
Out[1355]: 
   T$QOOR  ne  po
0       3   0   3
1      14   0  14
2      12   0  12
3      -6  -6   0
4     -19 -19   0
5       9   0   9

df.assign(po=df.where(df['T$QOOR']>0,0),ne=df.where(df['T$QOOR'])注意,新列中的负值变为正值
sls_item['SALES'] = sls_item['T$QOOR'].where(lambda x: x >= 0, 0)
sls_item['RETURN'] = sls_item['T$QOOR'].where(lambda x: x < 0, 0) * -1
print (sls_item)
   T$QOOR  SALES  RETURN
0       3      3       0
1      14     14       0
2      12     12       0
3      -6      0       6
4     -19      0      19
5       9      9       0
mask = sls_item['T$QOOR'] >=0
sls_item['SALES'] = np.where(mask, sls_item['T$QOOR'], 0)
sls_item['RETURN'] = np.where(~mask, sls_item['T$QOOR'] * -1, 0)
print (sls_item)
   T$QOOR  SALES  RETURN
0       3      3       0
1      14     14       0
2      12     12       0
3      -6      0       6
4     -19      0      19
5       9      9       0
df.assign(po=df.where(df['T$QOOR']>0,0),ne=df.where(df['T$QOOR']<0,0))
Out[1355]: 
   T$QOOR  ne  po
0       3   0   3
1      14   0  14
2      12   0  12
3      -6  -6   0
4     -19 -19   0
5       9   0   9