bbe*_*t36 2 python regex parsing wildcard pandas
初始数据(字符串数据类型)
Los Gatos 50K
Las Palmas Canary Islands 25K
Roland Garros
Seoul 25K
Rome
Run Code Online (Sandbox Code Playgroud)
期望的结果
Los Gatos
Las Palmas Canary Islands
Roland Garros
Seoul
Rome
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种方法来删除任何由 2 位数字和 K 组成的字符串模式。但它需要能够处理 K 之前的任何 2 个值。我还没有看到任何使用通配符作为部分的答案替换。应该是这样的(我知道这是无效的)-
data.replace("**K", '')
旁注 - 该字符串将是数据框中的一列,因此如果有一个简单的解决方案可以使用它,那将是理想的选择。如果没有,我可以迭代每一行并以这种方式对其进行转换。
尝试
df = df.replace('\d{2}K', '', regex = True)
0
0 Los Gatos
1 Las Palmas Canary Islands
2 Roland Garros
3 Seoul
4 Rome
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1799 次 |
| 最近记录: |