根据某一列过滤行,然后检查另一列的值是否在Python的特定列表中

阿邦

给定以下玩具构建数据,我想检查何时type == 'other'在数据框中是否存在一些不规则的值(表示inHallParking spacename

    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

对于上面的数据集,我希望它创建一个新列indication并返回N最后一行,因为Roberts PLC它不在中['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

我使用过的代码需要编辑:

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

感谢您提前的帮助。

加:

对于打印指示:

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')

出:

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

您可以直接进行分配:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Python Pandas DataFrame检查一列的值是否在另一列表中

检查列是否具有特定值,然后通过列表限制另一列

根据python中的另一列过滤列列表

Excel根据另一列中的值过滤列表

Pandas - 根据后一列中是否存在值过滤一列中具有相同值和另一列中多个值的行

如何检查一列的值是否在另一行的另一列中

检查一列中的值是否在Python中另一列的间隔值中

在某一列中求和,以得出另一列中包含相同值的行

如何在MySQL / PHP的某一列中检查是否存在具有特定值的行?

Python Pandas:检查一列中的值是否存在于另一列中的行子集中

从一列的列值中列出,然后过滤另一列的值

根据另一列中的重复值过滤一列中的唯一值

检查值是否包含在另一列中,然后根据该值(Excel)获取值

根据R中另一列中的特定值过滤一行行

通过Python中某一列的特定总和值选择前N行

如何根据另一列中的条件更改数据框的某一列中的值?

根据某一列中的特定模式提取数据

如何根据另一列中的值检查 pandas df 列值是否存在?

根据 SQL 中的另一列过滤值

函数根据R中另一列的数据检索某一列的数据

Excel,计算某一列的总和,其中另一列中的对应行等于一个值

如何从另一列中查找值并检查其是否重复,然后在Oracle SQL中删除行

检查一列中的一个值是否在另一列中

如何根据SQL中另一列的值过滤一列

对于每一行,检查另一列是否存在一列中的值

查找某一列值匹配而另一列不匹配的行

申请检查一列中的元素是否包含在另一列的列表中

在熊猫中填充另一列中某一列的缺失值

SQL-从一列中选择唯一值,然后根据另一列进行过滤