Reputation: 19
I have a dataframe as:
id | Value |
---|---|
0 | Auto |
1 | Manual |
2 | Auto |
3 | Manual |
4 | Manual |
I want to add another column based on values present in second column as:
id | Value | Flag |
---|---|---|
0 | Auto | Yes |
1 | Manual | |
2 | auto | Yes |
3 | manual | |
4 | Manual |
I tried below code:
mask = df['Purchase Order Num'].str.contains('Auto', na= False, case=False)
df['Flag'] = np.where(df[mask], 'Yes', '')
I get the error as :
Length of values (2) does not match length of index (5)
What went wrong?
Upvotes: 0
Views: 54
Reputation: 323226
When we doing the np.where
we need pass the whole Boolean to it
mask = df['Purchase Order Num'].str.contains('Auto', na= False, case=False)
df['Flag'] = np.where(mask, 'Yes', '')
pandas assign can do
df['Flag'] = ''
df.loc[mask, 'Flag'] = 'Yes'
Upvotes: 2