[英]Filter pd.DataFrame rows by list of the columns
我需要選擇其中一列中的值不等於0的行。這是代碼:
tmp = train[(train['col1'] > 0) | (train['col2'] > 0) |
(train['col3'] > 0) | (train['col4'] > 0) |
(train['col5'] > 0)]
我如何通過遍歷列的列表來實現相同的結果,例如['col1','col2',...,'col5']?
#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)]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.