pandas 分析某列的值是否小于另一列的值,以及此列的值是否小于另一列的值,依此类推

osh3o9ms  于 2023-01-15  发布在  其他
关注(0)|答案(2)|浏览(197)

目前我是这样做的:

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

对于这种总是使用相同模式并且只更改要分析的列的分析,有没有更正确的方法?

4xy9mtcn

4xy9mtcn1#

使用shift执行比较,并使用all聚合为单个布尔值以进行布尔索引:

out = dt[dt.shift(axis=1).lt(dt).iloc[:, 1:].all(axis=1)]

输出:

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
xxslljrj

xxslljrj2#

你可以从左到右取列差异,看看是否 * 所有 * 差异都是 less than 0,从而确定掩码:

dt.loc[dt.diff(-1, axis="columns").iloc[:, :-1].lt(0).all(axis="columns")]

.iloc[:, :-1]将丢弃最右边的差分结果NaNs,因为它没有右列。)
得到

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

相关问题