给定以下玩具构建数据,我想检查何时type == 'other'
,在数据框中是否存在一些不规则的值(表示inHall
或Parking space
)name
。
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] 删除。
我来说两句