Select rows where value of column A starts with value of column B

Viewed 1685

I have a pandas dataframe and want to select rows where values of a column starts with values of another column. I have tried the following:

import pandas as pd

df = pd.DataFrame({'A': ['apple', 'xyz', 'aa'],
                   'B': ['app', 'b', 'aa']})

df_subset = df[df['A'].str.startswith(df['B'])]

But it errors out and this solutions that I found also have not been helping.

KeyError: "None of [Float64Index([nan, nan, nan], dtype='float64')] are in the [columns]"

np.where(df['A'].str.startswith(df['B']), True, False) from here also returns True for all.

3 Answers

For row wise comparison, we can use DataFrame.apply:

m = df.apply(lambda x: x['A'].startswith(x['B']), axis=1)
df[m]

       A    B
0  apple  app
2     aa   aa

The reason your code is not working is because Series.str.startswith accepts a character sequence (a string scalar), and you are using a pandas Series. Quoting the docs:

pat : str
Character sequence. Regular expressions are not accepted.

You may need to do with for loop , since the row check is not support with str.startswith

[x.startswith(y) for x , y in zip(df.A,df.B)]
Out[380]: [True, False, True]
df_sub=df[[x.startswith(y) for x , y in zip(df.A,df.B)]].copy()

You can achieve this without using for loop:

import pandas as pd
import numpy as np

df = pd.DataFrame({'A': ['apple', 'xyz', 'aa'],
                   'B': ['app', 'b', 'aa']})

ufunc = np.frompyfunc(str.startswith, 2, 1)
idx = ufunc(df['A'], df['B'])
df[idx]

Out[22]: 
       A    B
0  apple  app
2     aa   aa
Related