[英]Find the index labels of rows that satisfies an if condition in a dataframe
I need to drop all the rows if the value of a string in a column begins with @ or Account.如果列中字符串的值以 @ 或 Account 开头,我需要删除所有行。
I used list comprehension and.index method but the list is not a index number.我使用了列表理解和.index 方法,但列表不是索引号。
I need index numbers of all rows to be dropped.我需要删除所有行的索引号。
x = [
i.index for i in df['Cleared/Open Items Symbol']
if isinstance(i, str) if i.startswith('@') or i.startswith('Account')
]
print(x)
Use str.contains
.使用str.contains
。 This is part of pandas
.这是pandas
的一部分。 By default it checks regular expressions.默认情况下,它检查正则表达式。 It will return True
or False
, ie type bool for all rows.它将返回True
或False
,即为所有行键入 bool。 As you want to drop those you negate it with the tilde operator and take the according elements from your dataframe.当你想删除那些你用波浪号运算符否定它并从你的 dataframe 中获取相应的元素时。 This can be written in a simple one-liner.这可以写成一个简单的单行。
dfnew = df[~df['columnName'].str.contains('^@|^Account')]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.