pythonpandasdataframe

How to select a range of data in a pandas dataframe


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


Solution

  • I will guess that this is what you want:

    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"]
    }
    
    df = pd.DataFrame(data)
    
    selected_month = "January"
    selected_month_idx = df[df["month"] == selected_month].index[0]
    six_months_indices = [i % len(df) for i in range(selected_month_idx - 2, selected_month_idx + 4)]
    six_months_df = df.loc[six_months_indices] # add .reset_index(drop=True) if needed
    

    Output:

       function service     month
    10   test11       E  November
    11   test12      YU  December
    0     test1       A   January
    1     test2       B  February
    2     test3      AO     March
    3     test4       M     April
    

    You will always get 6 months and selected month will always be third in those six months. Months will go in circle.

    Little note, if you want to selected month appear on some other place in those six months play around with bounds in range function. For example, if you want selected month to be first use range(selected_month_idx, selected_month_idx + 6) or if you want it to be last use range(selected_month_idx - 5, selected_month_idx + 1). For any in between or to change number of shown months play more with bounds.