質問するログイン新規登録

回答編集履歴

1

先読みアサーション追加

2021/12/27 00:06

投稿

ppaul
ppaul

スコア24672

answer CHANGED
@@ -23,6 +23,21 @@
23
23
  6 100 214.50円
24
24
  7 100 220円
25
25
  ```
26
+ 正規表現の先読みアサーションまで使うと以下のようになります。
26
27
 
28
+ ```python
29
+ >>> df['金額'] = df['金額'].str.replace('円(?=.)', '.', regex=True).str.replace('銭', '円')
30
+ >>> print(df)
31
+ 金額
32
+ 0 0 44円
33
+ 1 10 105.60円
34
+ 2 20 139.70円
35
+ 3 30 170.50円
36
+ 4 40 198円
37
+ 5 50 206.80円
38
+ 6 100 214.50円
39
+ 7 100 220円
40
+ ```
27
41
  参考
28
- [pandasの文字列メソッドで置換や空白削除などの処理を行う](https://note.nkmk.me/python-pandas-str-replace-strip-etc/)
42
+ [pandasの文字列メソッドで置換や空白削除などの処理を行う](https://note.nkmk.me/python-pandas-str-replace-strip-etc/)
43
+ [公式ドキュメント 正規表現のシンタックス](https://docs.python.org/ja/3/library/re.html#regular-expression-syntax)