[Python] How to retrieve one data value from the result of a pandas DataFrame.groupby().mean()

Discussão em 'Python' iniciado por Stack, Agosto 28, 2025.

  1. Stack

    Stack Membro Participativo

    Using Pandas 2.3.2 on Python 3.9.2 via JupyterLab.

    I've collected a bunch of thermal data from a thing. I've already collated that data into DataFrame chunks that look like this:

    zone data Setpoint
    9 zone1 40.34347 40
    13 zone1 40.07553 40
    17 zone1 39.98359 40
    21 zone1 40.06895 40
    25 zone1 40.04465 40
    .. ... ... ...
    952 zone4 109.91890 110
    956 zone4 109.90520 110
    960 zone4 110.00600 110
    964 zone4 110.02160 110
    968 zone4 109.94940 110


    Then I've used groupby and mean() to, well, group and create means:

    means = temps[['zone','Setpoint','data']].groupby(['zone','Setpoint']).mean()

    data
    zone Setpoint
    zone1 40 40.050959
    50 50.030125
    60 60.066517
    70 70.050257
    80 80.045247
    90 90.071484
    100 100.032826
    110 110.137990
    zone3 40 39.990645
    50 50.015407
    60 60.053120
    70 70.044470
    80 80.043304
    90 90.077433
    100 100.070493
    110 110.140510
    zone4 40 40.048473
    50 50.017906
    60 60.037044
    70 70.012458
    80 80.034280
    90 90.087850
    100 100.047793
    110 110.067390


    (Aside: I notice how "data" is a line above "zone" and "Setpoint", and I don't know what that's trying to tell me about the structure)

    I can grab one zone's worth of info by doing z1means = means.loc['zone1']:

    data
    Setpoint
    40 40.050959
    50 50.030125
    60 60.066517
    70 70.050257
    80 80.045247
    90 90.071484
    100 100.032826
    110 110.137990


    What I can't figure out is how to get at, e.g., the "data" value where "Setpoint" is 80:

    *some undiscovered syntax*

    80.045247

    I've come to understand that the "Setpoint" value is the index on the DataFrame object held in z1means but can't figure out how to refer to the data using it:

    <class 'pandas.core.frame.DataFrame'>
    Index: 8 entries, 40 to 110
    Data columns (total 1 columns):
    # Column Non-Null Count Dtype
    --- ------ -------------- -----
    0 data 8 non-null float64
    dtypes: float64(1)
    memory usage: 128.0 bytes
    None


    I can only imagine that the Index: 8 entries, 40 to 110 is telling me something important, but I can't figure out what that is or how to use it.

    Things I've tried and their results:

    z1means[80] -> KeyError: 80
    z1means.at(80) -> TypeError: '_AtIndexer' object is not callable
    z1means.loc(80) -> KeyError: 80, ValueError: No axis named 80 for object type DataFrame
    z1means.iloc(80) -> Same as .loc
    z1means.loc(z1means['Setpoint'] == 80) -> KeyError: Setpoint


    What am I just not grasping here?

    Continue reading...

Compartilhe esta Página