Split string column based on number of characters

Viewed 4227

I have the following column in a dataframe:

columnA
EUR590
USD680
EUR10000,9
USD40

how can i split it based on the first three characters, that the dataframe looks like:

columnA columnB
590     EUR
680     USD
10000,9  EUR
40       USD
2 Answers
df['columnB']=df['columnA'].str.slice(stop=3)
df['columnA']=df['columnA'].str.slice(start=3)

Another way would be using series.str.extract() with a pattern:

df1=df.columnA.str.extract('(?P<columnA>.{3})(?P<columnB>.{1,})')
print(df1)

  columnA  columnB
0     EUR      590
1     USD      680
2     EUR  10000,9
3     USD       40

Regex graph below:

Regex graph

Related