Reputation: 1
How can I take separate the cities and names based on the capital letters? Some cities only have 2 letters in their abbreviation.
'Kevin DurantBKN'
'Stephen CurryGS'
photo of the dataframe because it wasnt formatting correctly
Upvotes: 0
Views: 38
Reputation: 11602
You could use a regular expression to match two groups: the first one is a string prefix that ends with a lowercase character: ^(.*[a-z])
, the second is a non-empty string suffix that only contains uppercase characters: ([A-Z]+)$
.
In pandas, you could use extract
as follows:
df = pd.DataFrame({"String": ["Kevin DurantBKN", "Stephen CurryGS"]})
result = df.String.str.extract(r"^(.*[a-z])([A-Z]+)$")
print(result)
# 0 1
# 0 Kevin Durant BKN
# 1 Stephen Curry GS
Upvotes: 2