'How to separate text into two different columns (pandas)
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
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
