python - Trailing chars in Pandas Series - How to make the sign suffix a prefix?
问题描述
I have a Series set that contains negative values using a trailing minus, like that: 1.22-
.
I want to change those in an elegant and performant way to: -1.22
.
How can I do that.
I have tried this:
In [1]: pd.Series(['1.22-', '-9.99', np.nan]).str.replace('-$', '-^')
Which results in that:
Out[1]:
0 1.22-^
1 -9.99
2 NaN
dtype: object
解决方案
我会这样做:
negatives = series.str[-1] == '-'
series[negatives] = '-' + series[negatives].str[:-1]
推荐阅读
- email - 电子邮件中的 Base 64 编码图像
- r - 我将如何改变 ggplot 图例符号的形状?
- javascript - socket.io 管理员/用户的唯一颜色
- javascript - HLS.js 获取视频片段信息
- javascript - Price.map 不是函数
- concurrency - V-lang:如何每秒发送 +2500 个 HTTP 请求?
- reactjs - 如何在 React js 中使用数组将数组推送到对象中?
- javascript - 嵌入对象以显示 PDF 显示为不可见
- python - 无法导入类,即使快速修复找到它
- javascript - 修复 JavaScript LoadMore 按钮以每页加载 5 个项目 - Django