简体   繁体   中英

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
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:

正则表达式图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM