import pandas as pd
df=pd.DataFrame({'A':['my text your text']})
I want to extract part of string in column A
which is my text
to column B
and remove it from A
after extract.
First thing that I will do in that case is propably create B column and after that subtract B from A, but is there any better solution to do that?
df['B']=df['A'].str.extract('(my text)')
df
A B
0 my text your text my text
Expected output:
A B
0 your text my text
Use Series.str.replace
with \s*
for remove trailing whitespaces and n=1
for replace only first match:
df['B']=df['A'].str.extract('(my text)')
df['A']=df['A'].str.replace('\s*(my text)\s*','', n=1)
print (df)
A B
0 your text my text
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With