[Python] When trying to index from a series, why it just showed one element instead of one row?

Discussão em 'Python' iniciado por Stack, Abril 24, 2025.

  1. Stack

    Stack Membro Participativo

    i am working on a python practice. i am trying to filter out which occupation has the lowest average sleeping duration from data csv. Here is example code.

    import pandas as pd
    sleep_data = pd.read_csv('sleep_health_data.csv')
    sleep_duration = sleep_data.groupby('Occupation')['Sleep Duration'].mean()
    lowest_sleep_occ=sleep_duration.sort_values().index[0]
    print(lowest_sleep_occ)


    the final result of lowest_sleep_occ shows 'Sales Representative'. i do not understand logically why it just showed one elemententer image description here. Because i tried to seek out sleep_duration which is a data series. so the structure looks like this enter image description here. then i think when index[0], it means indexing the first whole row, right? the result was supposed to be 'Sales Representative 5.900000'. i wonder where the figure is gone?

    Continue reading...

Compartilhe esta Página