我想返回符合特定条件的行。我可以对单行执行此操作,但我需要对多行组合执行此操作。例如,“浅绿色”符合“XYZ”为正且“总计”> 10 的条件,而“红色”则不然。当我组合相邻的一行或多行时,它确实=>“深绿色”。我可以实现遍历所有行而不返回重复行吗?
N = 1000
np.random.seed(0)
df = pd.DataFrame(
{'X':np.random.uniform(-3,10,N),
'Y':np.random.uniform(-3,10,N),
'Z':np.random.uniform(-3,10,N),
})
df['total'] = df.X + df.Y + df.Z
df.head(10)
![enter image description here](https://i.stack.imgur.com/9kL6Z.png)
EDIT;
所需的输出为 'XYZ'> 0 且 'total' > 10
这是一个尝试。你可能想使用rolling
or expanding
(为了速度和优雅)而不是显式循环range
,但我这样做是为了能够打印出用于计算每个布尔值的行。
df = df[['X','Y','Z']] # remove the "total" column in order
# to make the syntax a little cleaner
df = df.head(4) # keep the example more manageable
for i in range(len(df)):
for k in range( i+1, len(df)+1 ):
df_sum = df[i:k].sum()
print( "rows", i, "to", k, (df_sum>0).all() & (df_sum.sum()>10) )
rows 0 to 1 True
rows 0 to 2 True
rows 0 to 3 True
rows 0 to 4 True
rows 1 to 2 False
rows 1 to 3 True
rows 1 to 4 True
rows 2 to 3 True
rows 2 to 4 True
rows 3 to 4 True
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)