[英]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.