简体   繁体   English

熊猫返回具有匹配子字符串到字符串的行

[英]Pandas return rows with matching substring to string

I have a dataframe where i want to check for every row if one column of substring is contained in another column of string.我有一个数据框,如果一列子字符串包含在另一列字符串中,我想检查每一行。 If a character in Subposition column exists in the string of Position column, return True, other wise return False in the Check column.如果 Position 列的字符串中存在 Subposition 列中的字符,则返回 True,否则返回 Check 列中的 False。 (L = left, R = Right , S = straight, U = u turn) (L = 左,R = 右,S = 直,U = u 转弯)

 Position   Subposition  Check
   L           R         False 
   L           L         True
   LR          L         True
   SRU         L         False

You can zip the 2 columns and check if the second row is in the first via a list comprehension which should be pretty fast:您可以zip 2 列并通过列表理解检查第二行是否在第一行中,这应该非常快:

df['Check' ] = [b in a for a,b in zip(df['Position'],df['Subposition'])]
print(df)

  Position Subposition  Check
0        L           R  False
1        L           L   True
2       LR           L   True
3      SRU           L  False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM