I have a dataframe, I need to select a range of data based on a month value, but the result expected is always showing six rows where the month selected appears in the filtered data , here's the code : 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 print(six_months_df) 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 the issue with this code is when I select January for example, the order of the months is not good, what I expet is somthing like that : function service month 0 test1 A January 1 test2 B February 2 test3 AO March 3 test4 M April 3 test5 A May 3 test6 PO June when I select December for example the output should be : function service month 6 test7 MP July 7 test8 YU August 8 test9 Z September 9 test10 R October 10 test11 E November 11 test12 YU December when I select Octobre the output should be for example : function service month 6 test7 MP July 7 test8 YU August 8 test9 Z September 9 test10 R October 10 test11 E November 11 test12 YU December the order of the month displated matters, always min(month) => max(month) output like this is not expected : 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 anyone please could to adjust the code above, thanks Continue reading...