python - 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?
解决方案
您想-
在任何用至少一个括号括起来的数字之前添加一个,该括号位于左侧、右侧或两侧。
使用基于交替的正则表达式:
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+ 个空格和一个可选)
字符
推荐阅读
- javascript - 如何在图表js的Y轴上应用美元符号?
- angular - 在项目中使用 ng6-toastr 后,请在规范文件 jasmine 中使用 setRootViewContainerRef 设置根 ViewContainerRef
- javascript - 专注于模糊上新创建的输入字段
- javascript - 事件监听器没有触发?
- c++ - 无法使用 soci 库连接到 SQL Server
- c# - 检测无 BOM 或有 BOM 的 utf-8
- javascript - 避免在ajax中等待其他进程完成
- json - 如果对象的所有字段都为空,如何将对象设置为空?
- python - 如何在 Google Colab 中安装 CUDA - 没有 ATen_cuda 库无法初始化 CUDA
- java - 如何在 Java 的整个代码中使用一个变量的值?