首页 > 解决方案 > Pandas - Induce an opening/closing bracket or induce a Negative sign before the number

问题描述

I have a column which has data with circular braces missing at times: The output required is to either remove the braces and induce a negative sign before the number or add starting/ending braces if it's missing any. EX - "10,752)" to "-10,752" or "(10,752)"

input -

 0. 21,028 
 1. 11,689 
 2. 94 
 3. 10,572) 
 4. 2,261

Name: Sale , dtype: object

Output required

either -

 0. 21,028 
 1. 11,689 
 2. 94 
 3. (10,572) 
 4. 2,261

OR

 0. 21,028 
 1. 11,689 
 2. 94 
 3. -10,572 
 4. 2,261

Is it possible? Or, you can consider it as a String like this:

testz = '21,028 \n1    11,689 \n2        94 \n3    10,572) \n4     2,261 \n

Output Req'd:

21,028 \n1    11,689 \n2        94 \n3    (10,572) \n4     2,261 \n

OR

21,028 \n1    11,689 \n2        94 \n3    -10,572 \n4     2,261 \n

As you can see "10572)" is replaced with (10572) or -10572

Now I know how to remove the brackets all together - re.sub(r'[()]', r'', testz)

but to induce one or replace the number with negative I'm not sure how.

if i add a new input:

0.        80,123 
1.         5,060 
2.      (4,756 ) 
3.     ( 16,572) 
4.    ( 14,673 )
Name: sale , dtype: object

or

testz = '80123 \n1    5,060 \n2        (4756) \n3    (16,572) \n4     (14,673) \n '
testz.replace(r'\((\d+(?:\.\d+)?)\b(?!\))|\b(?<!\()(\d+(?:\.\d+)?)\)', r'-\1\2')

stops working. Do you have any idea what could be wrong?

标签: pythonregexpandas

解决方案


您想-在任何用至少一个括号括起来的数字之前添加一个,该括号位于左侧、右侧或两侧。

使用基于交替的正则表达式:

df['testz'].str.replace(r'\(?\s*(\d+(?:,\d+)?)\s*\)|\(\s*(\d+(?:,\d+)?)\s*\)?', r'-\1\2')

查看正则表达式演示

细节

  • \(?\s*(\d+(?:,\d+)?)\s*\)- 一个可选(的 0+ 空格,第 1 组:1+ 数字后跟 1+ 数字的可选序列,;然后是 0+ 个空格和一个)字符
  • |- 或者
  • \(\s*(\d+(?:,\d+)?)\s*\)?- 一个(字符,0+ 个空格,第 1 组:1+ 个数字后跟 1+ 个数字的可选序列,;然后是 0+ 个空格和一个可选)字符

推荐阅读