简体   繁体   English

遍历 dataframe 中的所有行并检查所有列值是否在列表中

[英]Iterate over all rows in dataframe and check all column values are in list

I have a dataframe with 7 columns and ~5.000 rows.我有一个 dataframe 有 7 列和 ~5.000 行。 I want to check that all the column values in a row are in my list and if so either add them to a new dataframe OR remove those where all values do not match, ie remove false rows (w/e is the easiest);我想检查一行中的所有列值是否都在我的列表中,如果是,请将它们添加到新的 dataframe 或删除所有值不匹配的那些,即删除错误行(w/e 是最简单的);

for row in df:
   for columns in row:
      if df.iloc[row, column].isin(MyList):
         ...*something*

I could imagine that .apply and .all could be used, but I'm afraid my python skills are limited, any help?我可以想象.apply.all可以使用,但恐怕我的 python 技能有限,有什么帮助吗?

If I understood correctly, you can solve this by using apply with a lambda expression like:如果我理解正确,您可以使用apply和 lambda 表达式来解决这个问题,例如:

df.loc[df.apply(lambda row: all(value in MyList for value in row), axis=1))]

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

相关问题 遍历 pandas 数据框中的行。 如果特定列之前存在空白,则将所有列值移到 - Iterate over rows in pandas dataframe. If blanks exist before a specific column, move all column values over 检查列表的所有值是否都在数据框列中 - check if all the values of list is in dataframe column 迭代行并将值附加到数据框中的列 - Iterate over rows and append values to column in dataframe 如何迭代 dataframe 以便将具有共同特定列值的所有行保存到各自的文件中? - How to iterate over dataframe such that all rows which have a specific column value in common are saved to their respective files? 选择列表中的Pandas DataFrame列值的所有行 - Select ALL rows where Pandas DataFrame Column values in a List 遍历 Pandas DataFrame 中的行,删除特定字符串后指定列数内的所有值 - Iterate Over Rows in Pandas DataFrame Deleting All Values Within a Specified Number of Columns After a Specific String 如何在 dataframe 中在一列中的两个值之间迭代行? - How to iterate over rows in a dataframe between two values in one column? 遍历 dataframe 的所有行 - iterate trough all rows of dataframe 遍历列表中的所有元素以在 Pandas 中找到 dataframe 中的行索引 - Iterate over all the elements in a list to find the row indexes in a dataframe in Pandas 检查数据框列中的所有值是否相同 - Check if all values in dataframe column are the same
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM