pandas DataFrame多行,上行下行相互比较 2行比较

使用loc:

          Lx     A      Ratio
upper  0.6  0.36   0.00
value  0.6  0.36   0.00
lower  0.9  0.81   0.85

upper是上限,lower是下限,找出value符合这个区间的值。

df.loc['upper'] > df.loc['value']

# -*- coding:utf-8 -*-
# @Time    : 2022/10/15 21:40
# @Author  : Lani
# @File    : test3.py

import pandas as pd
import numpy as np

if __name__ == "__main__":
    DataExample = [[0.6, 0.36, 0.00],
                   [0.6, 0.36, 0.00],
                   [0.9, 0.81, 0.85]
                 ]

    df = pd.DataFrame(data=DataExample, index=['upper','value','lower' ],columns=['Lx', 'A', 'Ratio'])
    print(df)
    print("-----")
    # df['match'] = df.Lx.eq(df.Lx.shift())
    # print(df)
    diff = (df.loc['upper'] > df.loc['value']) & (df.loc['value'] > df.loc['lower'])
    print(diff)
    # df = df.loc[diff]
    df.loc['result'] = diff  # 赋值到新行
    print(df)

比对结果放在新的行:

Lx       False
A        False
Ratio    False
dtype: bool

结果 :

         Lx     A  Ratio
upper   0.6  0.36   0.00
value   0.6  0.36   0.00
lower   0.9  0.81   0.85
result  0.0  0.00   0.00

你可能感兴趣的:(python,pandas,python,数据分析)