[Python] How to select a range of data in a pandas dataframe

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

  1. Stack

    Stack Membro Participativo

    I have this pandas dataframe : df :

    import pandas as pd

    data = {

    "function": ["test1","test2","test3","test4","test5","test6","test7","test8","test9","test10","test11","test12",

    ],
    "service": ["A", "B", "AO", "M" ,"A", "PO", "MP", "YU", "Z", "R", "E", "YU"],
    "month": ["January","February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
    }

    #load data into a DataFrame object:
    df = pd.DataFrame(data)

    print(df)


    the result :

    function service month
    0 test1 A January
    1 test2 B February
    2 test3 AO March
    3 test4 M April
    4 test5 A May
    5 test6 PO June
    6 test7 MP July
    7 test8 YU August
    8 test9 Z September
    9 test10 R October
    10 test11 E November
    11 test12 YU December


    I have a slider which has a variable where i can select a month, imagine this variable called var. now what I'm looking for is, when I select a month in the slider I want to filter the dataframe but i want to get always six rows where the month selected is appeared in the filtered data(wherever where it appeared in the result, in begining or in the middle or at the end)

    if you could please help ?

    which i have triend :

    def selectDataRange(var:str,df):
    if var=="January":
    df.iloc[0: 6,]
    if var=="February":
    df.iloc[1: 6,]
    if var=="March":
    df.iloc[2: 6,]


    i have tried this methode(only for the third months)..but it doesnt work

    Continue reading...

Compartilhe esta Página