[Python] How to calculate with MultIndex?

Discussão em 'Python' iniciado por Stack, Novembro 21, 2024.

  1. Stack

    Stack Membro Participativo

    I have a Pandas DataFrame with coordinates values (X,Y), temperatures (T) and measurement results (H)

    df=pd.DataFrame([
    {'X':0,'Y':0,'T':25,'H':1.2},
    {'X':1,'Y':0,'T':25,'H':1.3},
    {'X':0,'Y':1,'T':25,'H':1.4},
    {'X':1,'Y':1,'T':25,'H':1.5},
    {'X':0,'Y':0,'T':125,'H':2.2},
    {'X':1,'Y':0,'T':125,'H':2.4},
    {'X':0,'Y':1,'T':125,'H':2.6},
    {'X':1,'Y':1,'T':125,'H':2.8},
    ])
    df=df.set_index(['X','Y','T'])


    Now I would like to calculate the difference of the measurements at the temperatures of each coordinate point: H(T=125)-H(T=25) So I would like to get

    {'X':0,'Y':0,'dH':1.0}
    {'X':1,'Y':0,'dH':1.1}
    {'X':0,'Y':1,'dH':1.2}
    {'X':1,'Y':1,'dH':1.3}


    How to do this in a pythonic way?

    Continue reading...

Compartilhe esta Página