[英]Subsetting pandas dataframe for identical value on several consecutive columns
我有以下類型的 pandas 數據框;
id | a | b | c | d | e | g
---------------------------
1 | 0 | 1 | 0 | 1 | 1 | 1
2 | 0 | 0 | 0 | 0 | 1 | 0
3 | 0 | 0 | 0 | 0 | 1 | 1
我想過濾所有列為零的行, a
到d
。 我知道這可以通過以下方式實現;
data[data['a']==0 & data['b']==0 & data ['c'] ==0 & data ['d'] ==0]
但是有沒有更快的方法使用iloc
或loc
來實現相同的目標。 例如,我嘗試了以下方法;
data.iloc[:,[1:4]==0]
但它給了我以下錯誤信息; ValueError: Can only index by location with a [integer, integer slice (START point is INCLUDED, END point is EXCLUDED), listlike of integers, boolean array]
任何可以替代我原始解決方案的替代反饋/建議表示贊賞。謝謝
你的意思是:
df[df.iloc[:,1:4].eq(0).all(1)]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.