I have a dataframe:
df = pd.DataFrame([['Jim', 'CF'], ['Toby', 'RW'], ['Joe', 'RWF'], ['Tom', 'RAMF'], ['John', 'RWB']], columns=['Name', 'Position'])
I want to obtain a subset of this dataframe such that we only have subjects who:
- Has
Position ='RW', 'RWF', or 'RAMF'
I need to do this in one line of code I can currently do this in two lines:
RW = df[df['Position'].str.startswith(('RW', 'RAMF', 'RWF'), na = False)]
RW = RW[RW['Position'].str.contains('RWB')==False]
The issue is that subjects with position 'RWB' show up when subsetting by str.startswith('RW'). Therefore, I have to specify in the second line to remove these 'RWB'.
Is it possible to do this in one line of code??
If need test starting of strings use:
Or:
If need test if exist values of tuple in column: