Anthony J
Anthony J

Reputation: 1

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

Upvotes: 0

Views: 38

Answers (1)

hilberts_drinking_problem
hilberts_drinking_problem

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

Related Questions