按列列表过滤pd.DataFrame行

时间:2017-05-20 21:09:51

标签: python pandas dataframe

我需要选择其中一列中的值不等于0的行。以下是代码:

asset

如何通过迭代列列表来完成相同的结果,例如['col1','col2',...,'col5']?

1 个答案:

答案 0 :(得分:2)

#use .any to check if any of the elements in a row is greater than 0.
train = train[(train>0).any()]

#if only need to check certain columns, this should work.
train[(train[['col1', 'col2', ..., ]]>0).max(1)]