简体   繁体   English

在pandas数据框中,如何检查同一行但不同列中是否存在两个字符串?

[英]In a pandas dataframe, how do I check if two strings exist on same row but in different columns?

So I have been trying to figure out how to write the simplest if statement in order to check if the string "A" exist in the rootID and "B" exist in the parentID in any of the rows. 因此,我一直在尝试找出如何编写最简单的if语句,以便检查在任何行中rootID中是否存在字符串“ A”,在parentID中是否存在“ B”字符串。 I then want to remove that row. 然后,我想删除该行。 In the following dataframe I would have wanted to remove row 0 in that case. 在以下数据框中,我本想在这种情况下删除第0行。

                     rootID   parentID    jobID  time
                  0    A         B          D    2019-01-30 14:33:21.339469
                  1    E         F          G    2019-01-30 14:33:21.812381
                  2    A         C          D    2019-01-30 15:33:21.812381
                  3    E         E          F    2019-01-30 15:33:21.812381
                  4    E         F          G    2019-01-30 16:33:21.812381

I know how to check if one element exists such as 我知道如何检查是否存在一个元素,例如

   if df['rootID'].str.contains("A").any()

but how do I do it when I need to check for two different strings in two columns? 但是当我需要在两列中检查两个不同的字符串时该怎么办?

Use boolean indexing with masks chained by | 使用boolean indexing和由|链接的掩码 for bitwise OR and ~ for invert boolean masks. 用于bitwise OR~用于反转布尔掩码。

If need check substrings: 如果需要检查子字符串:

m1 = ~df['rootID'].str.contains("A")
m2 = ~df['parentID'].str.contains("B")

If need check strings use Series.ne : 如果需要检查字符串,请使用Series.ne

m1 = df['rootID'].ne("A")
m2 = df['parentID'].ne("B")

#alternatives
#m1 = df['rootID'] != "A"
#m2 = df['parentID'] != "B"

df = df[m1 | m2]

print (df)
  rootID parentID jobID                        time
1      E        F     G  2019-01-30 14:33:21.812381
2      A        C     D  2019-01-30 15:33:21.812381
3      E        E     F  2019-01-30 15:33:21.812381
4      E        F     G  2019-01-30 16:33:21.812381

Another solution: 另一个解决方案:

df = df.query('rootID != "A" | parentID != "B"')

暂无
暂无

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

相关问题 如何检查Pandas中另一个dataframe中是否存在两列的组合值? - How to check if a combined value of two columns exist in another dataframe in Pandas? Pandas Dataframe:如何比较一行的两列中的值是否等于后续行的同一列中的值? - Pandas Dataframe: how can i compare values in two columns of a row are equal to the ones in the same columns of a subsequent row? 如何对两个不同的 pandas 列中的字符串进行部分匹配检查? - How do to a partial match check across strings in two different pandas columns? 如何将同一 position 中的列与两个不同的 pandas dataframe 相乘? - How to multiply columns in the same position from two different pandas dataframe? 比较熊猫中的两列。 如何检查哪一行不同? - Compare two columns in pandas. How to check which row is different? 熊猫:如何检查同一数据框中各列之间的值匹配? - Pandas: How do I check for value match between columns in same dataframe? 如何在 pandas dataframe 的每一行中的选定列中找到两个最低值? - How do I find the two lowest values across selected columns in each row of a pandas dataframe? 熊猫删除行,除非来自另一个数据帧中两列的字符串 - pandas delete row unless strings from two columns in another dataframe 如何检查 pandas dataframe 中的列之间的冲突? - How do I check for conflict between columns in a pandas dataframe? 如何检查PANDAS DataFrame列中是否包含一系列字符串,并将该字符串分配为行中的新列? - How to check if a series of strings is contained in a PANDAS DataFrame columns and assign that string as a new column in the row?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM