Home > Software design >  How to use re to split the column containg string combine integer without contain symbol
How to use re to split the column containg string combine integer without contain symbol

Time:11-08

Here is a column in df which contains integer and string both combine together. (even here's numbers type are string)

I want split the df['symbol'] into df['num'] and df['name'],how can I do this question by re.

QUESTION:

df = pd.DataFrame({'symbol': ['12345abc', '2234bcd', '323456cde'],'date':[5, 6, 7]})

ideal:

df1 = pd.DataFrame({'symbol': ['12345', '2234', '323456'],
               'name':['abc','bcd','cde'],
               'date':[5, 6, 7]})

Thanks to instructor.

CodePudding user response:

Can you try this

df[["", "symbol", "name"]] = df["symbol"].astype(str).str.split("(\d )", expand=True)
  • Related