How to use re to split the column containg string combine integer without contain symbol
Question:
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.
Answers:
Can you try this
df[["", "symbol", "name"]] = df["symbol"].astype(str).str.split("(d+)", expand=True)
split
is not the best tool for this, rather use extract
:
df[['symbol', 'name']] = df['symbol'].str.extract(r'(d+)(D+)')
Output:
symbol date name
0 12345 5 abc
1 2234 6 bcd
2 323456 7 cde
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.
Can you try this
df[["", "symbol", "name"]] = df["symbol"].astype(str).str.split("(d+)", expand=True)
split
is not the best tool for this, rather use extract
:
df[['symbol', 'name']] = df['symbol'].str.extract(r'(d+)(D+)')
Output:
symbol date name
0 12345 5 abc
1 2234 6 bcd
2 323456 7 cde