简体   繁体   English

在 dataframe 中查找满足 if 条件的行的索引标签

[英]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.它将返回TrueFalse ,即为所有行键入 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.

相关问题 如何遍历 dataframe 到 select 满足条件的行,包括在 python 中的索引 - How to Iterate through a dataframe to select rows that satisfies a condition including their index in python 当某列满足基于另一列的特定条件时,是否有一种方法可以迭代地找到数据帧的索引? - Is there a way to iteratively find index of a dataframe when a column satisfies a certain condition based on another column? 尝试遍历熊猫数据框的行并在满足条件时编辑行 - Trying to iterate through rows of pandas dataframe and edit row if it satisfies a condition select 行在不满足条件的行之前按 dataframe 分组(python) - select rows in group by dataframe before the row which not satisfies a condition (python) 特定行满足给定条件的所有列的索引 pandas - Index of All columns Whose Particular rows Satisfies Given Condition pandas Pandas DataFrame 在满足条件的前一行中查找最近的索引 - Pandas DataFrame find closest index in previous rows where condition is met 选择满足python条件的索引 - Selecting the index that satisfies a condition in python 如何根据索引然后根据条件选择数据帧的行? - How to select rows of dataframe based on index then on condition? 遍历 dataframe 以找到满足每组 id 条件的第一行 - Iterate through dataframe to find first row that satisfies condition for each group of id 测试 pandas DataFrame 的任何列是否满足条件 - Test if any column of a pandas DataFrame satisfies a condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM