Python 仅当特定列中出现子字符串(或符号)时,才将值复制到另一列,否则请保持另一列不变DataFrame

Python 仅当特定列中出现子字符串(或符号)时,才将值复制到另一列,否则请保持另一列不变DataFrame,python,pandas,Python,Pandas,我拥有的数据帧: cost total null $519 null $78 xx24 ($1500) $51 0.00 ($924) $33 $78 期望的: cost total null $519 null $78 xx24 ($1500) $1500 $51 0.00 ($924) $924 $33 $3

我拥有的数据帧:

cost      total     
null      $519
null      $78
xx24
($1500)   
          $51
0.00    
($924)
$33
          $78
期望的:

cost      total     
null      $519
null      $78
xx24
($1500)   $1500
          $51
0.00    
($924)    $924
$33       $33
          $78
我尝试定义方法并使用apply(),但这也将替换'total'中已经存在的值。
我可以在新列中获取“真/假”值,但这似乎不是正确的方法。

您可以提取
()
之间的值,但只能提取由中选择的
$
行的值:

或者,如果可能,用从
()
提取的值替换
总计中缺少的值,使用:

df['total'] = df['total'].fillna(df['cost'].str.extract(r"\((.*?)\)" , expand=False))
print (df)
      cost  total
0      NaN   $519
1      NaN    $78
2     xx24    NaN
3  ($1500)  $1500
4      NaN    $51
5     0.00    NaN
6   ($924)   $924
7      NaN    $78

您可以使用numpy.where()实现


发生了什么子字符串?它可以是“$”。。
df['total'] = df['total'].fillna(df['cost'].str.extract(r"\((.*?)\)" , expand=False))
print (df)
      cost  total
0      NaN   $519
1      NaN    $78
2     xx24    NaN
3  ($1500)  $1500
4      NaN    $51
5     0.00    NaN
6   ($924)   $924
7      NaN    $78
df['total'] = np.where(df.cost.apply(lambda x:not pd.isnull(x) and '$' in x),
                  df.cost,
                  df.total)
df['total'] = df.total.apply(lambda x:x.replace('(', '').replace(')', ''))