ah bon
ah bon

Reputation: 10061

Filter rows based on one column then check if another column's values are in the specific list in Python

Given a toy building data as follow, I want to check when type == 'other', whether there are some irregular values (which means not in Hall or Parking space) for name in the dataframe.

    id    type                          name
0    1  office  Hessel, Macejkovic and Nader
1    2  office                Stiedemann LLC
2    3  office                     Grant Ltd
3    4  office                Anderson Group
4    5  retail                   MacDanald's
5    6  retail                      Wallmart
6    7  retail                      Wallmart
7    8   other                          Hall
8    9   other                 Parking space
9   10   other                 Parking space
10  11   other                   Roberts PLC

For the dataset above, I hope it create a new column indication and returns N for the last row, since Roberts PLC is not in ['Hall', 'Parking space'].

    id    type                          name indication
0    1  office  Hessel, Macejkovic and Nader        NaN
1    2  office                Stiedemann LLC        NaN
2    3  office                     Grant Ltd        NaN
3    4  office                Anderson Group        NaN
4    5  retail                   MacDanald's        NaN
5    6  retail                      Wallmart        NaN
6    7  retail                      Wallmart        NaN
7    8   other                          Hall        NaN
8    9   other                 Parking space        NaN
9   10   other                 Parking space        NaN
10  11   other                   Roberts PLC          N

Code I have used which need to edit:

m = df1.loc[df1['type'].isin(['other'])]
if m['name'].str.contains('Hall|Parking space', na = False).any():
    print('')
else:
    print('N')

Thanks for your kind help at advance.

Plus:

For print the indication:

if (df["type"].eq("other")) & (~df["name"].str.contains('Hall|Parking space', na = False).any()):
    print('Other type data has irregular data')
else:
    print('No irregular data found in other type data')

Out:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Upvotes: 1

Views: 45

Answers (1)

Henry Yik
Henry Yik

Reputation: 22503

You can do the assignment directly:

df.loc[(df["type"].eq("other"))&(~df["name"].str.contains('Hall|Parking space', na = False)), "indication"] = "N"

print (df)

    id    type                          name indication
0    1  office  Hessel, Macejkovic and Nader        NaN
1    2  office                Stiedemann LLC        NaN
2    3  office                     Grant Ltd        NaN
3    4  office                Anderson Group        NaN
4    5  retail                   MacDanald's        NaN
5    6  retail                      Wallmart        NaN
6    7  retail                      Wallmart        NaN
7    8   other                          Hall        NaN
8    9   other                 Parking space        NaN
9   10   other                 Parking space        NaN
10  11   other                   Roberts PLC          N

Upvotes: 2

Related Questions