如何删除由ch拆分的字符串中不需要的字母

2024-04-20 09:07:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个nba球队的数据框,我想删除所有不需要的第一个字母或前面的特殊字符。你知道吗

Teams              |  W  |   L  |   Pct

*-Houston Rockets  | 65  |  17  |  0.793 

y -Golden State    | 58  |  24  |  0.707

x -Boston Celtics  | 55  |  27  |  0.671

x -Philadelphias   | 52  |  30  |  0.634

e -Denver Nuggets  | 46  |  36  |  0.561

要成为这样:

Teams           |  W   |   L  | Pct

Houston Rockets |  65  |  17  |  0.793 

Golden State    | 58   |  24  |  0.707

Boston Celtics  | 55   |  27  |  0.671

Philadelphias   | 52   |  30  |  0.634

Denver Nuggets  | 46   |  36  |  0.561

Tags: 数据字母bostonstatenba球队teamsnuggets
1条回答
网友
1楼 · 发布于 2024-04-20 09:07:48

您可以使用^{}来实现:

import pandas as pd

s = pd.Series(['*-Houston Rockets', 'y -Golden State',
               'x -Boston Celtics', 'x -Philadelphias',
               'e -Denver Nuggets'])

res = s.str.rsplit('-', 1).str[-1]

print(res)

# 0    Houston Rockets
# 1       Golden State
# 2     Boston Celtics
# 3      Philadelphias
# 4     Denver Nuggets
# dtype: object

相关问题 更多 >