Pandas split one series based on another series in the same dataframe

Consider the below dataframe.

data = [
    ("flat 1 35 rosendale road, london","rosendale road"),
    ("34 hackney road, london","hackney road"),
    ("33-45 london road, london", "london road")
    
]
df = pd.DataFrame(data=data, columns=['addr', 'street'])

             addr                           street
0   flat 1 35 rosendale road london     rosendale road
1   34 hackney road, london             hackney road
2   33-45 london road, london           london road

I'm trying to produce a final series that will split the text for each row in series addr based on series street, keeping only the first left split. Desired output below:

0    flat 1 35
1           34
2        33-45

So I've only managed to achieve this via looping(see below).Any suggestions on how to prevent looping (or apply using axis=1)?

# list comp solution
splits = pd.Series([
    row.addr.split(row.street)[0]
    for row in df.itertuples()
])

pd.__version__ -> 1.2.3



Read more here: https://stackoverflow.com/questions/67929328/pandas-split-one-series-based-on-another-series-in-the-same-dataframe

Content Attribution

This content was originally published by Francisco at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: