目前我是这样做的:
import pandas as pd
dt = pd.DataFrame({
'1st':[1,0,1,0,1],
'2nd':[2,1,2,1,2],
'3rd':[3,0,3,2,3],
'4th':[4,3,4,3,4],
'5th':[5,0,5,4,5],
'minute_traded':[6,5,6,5,6]
})
dt = dt[
(dt['1st'] < dt['2nd']) &
(dt['2nd'] < dt['3rd']) &
(dt['3rd'] < dt['4th']) &
(dt['4th'] < dt['5th']) &
(dt['5th'] < dt['minute_traded'])
]
print(dt)
结果:
1st 2nd 3rd 4th 5th minute_traded
0 1 2 3 4 5 6
2 1 2 3 4 5 6
3 0 1 2 3 4 5
4 1 2 3 4 5 6
对于这种总是使用相同模式并且只更改要分析的列的分析,有没有更正确的方法?
2条答案
按热度按时间4xy9mtcn1#
使用
shift
执行比较,并使用all
聚合为单个布尔值以进行布尔索引:输出:
xxslljrj2#
你可以从左到右取列差异,看看是否 * 所有 * 差异都是 less than 0,从而确定掩码:
(
.iloc[:, :-1]
将丢弃最右边的差分结果NaNs,因为它没有右列。)得到