PV8
PV8

Reputation: 6260

Split string column based on number of characters

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

Upvotes: 4

Views: 8773

Answers (2)

anky
anky

Reputation: 75080

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

Upvotes: 1

user8560167
user8560167

Reputation:

df['columnB']=df['columnA'].str.slice(stop=3)
df['columnA']=df['columnA'].str.slice(start=3)

Upvotes: 6

Related Questions