I'm trying to create a new column in a pandas dataframe based on whether a string is contained in another column. I'm using np.select based on this post. Here is an example dataframe and an example function to create the new column
df=pd.DataFrame({'column':['one','ones','other','two','twos','others','three','threes']})
def add(df):
conditions = [
('one' in df['column']),
('two' in df['column']),
('three' in df['column']),
('other' in df['column'])]
choices = [1, 2, 3, 0]
df['Int'] = np.select(conditions, choices, default=0)
return df
new_df=add(df)
The output I'm getting is
column Int
0 one 0
1 ones 0
2 other 0
3 two 0
4 twos 0
5 others 0
6 three 0
7 threes 0
And what I want is
column Int
0 one 1
1 ones 1
2 other 0
3 two 2
4 twos 2
5 others 0
6 three 3
7 threes 3
what am I doing wrong?