Assume we have dataframe df
:
import pandas as pd
df = pd.DataFrame({'txt': ["Building = Building_A and Floor = Floor_4",
"Building = Building_Z and Floor = Floor_9",
"Building = Martello and Floor = Ground"]})
First define pattern to extract:
pat = "(Floor_d+)|(Building_w{1})"
Alternatively if You look for all words after "= "
:
pat = r"(?<== )(w+)"
Please note lookbehind (?<=)
in pattern definition.
Then apply lambda function to column txt
:
df['txt_extract'] =
df[['txt']].apply(lambda r: "/".join(r.str.extractall(pat).stack()), axis=1)
Result:
0 Building_A/Floor_4
1 Building_Z/Floor_9
2 Martello/Ground
Instead of str.extract
use str.extractall
which looks for all occurences of pattern. Resulting searches are stacked and joined with "/"
separator.
Please note that order of patterns found is preserved what may be important in Your case.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…