How do I get the first element in a rolling window on Pandas data frame

I have the following data structure: and what I’d like to do is for each location, date to determine what was the spending exactly n days ago. I am trying with a rolling window here but for some reason, when I create the window, I don’t know how to get the first element in it:… Read More How do I get the first element in a rolling window on Pandas data frame

Pandas – compute previous custom quarter wise total revenue and reshape table

I have a dataframe like as below df = pd.DataFrame( {‘stud_id’ : [101, 101, 101, 101, 101, 101, 101, 101], ‘sub_code’ : [‘CSE01’, ‘CSE01’, ‘CSE01’, ‘CSE01’, ‘CSE02’, ‘CSE02’, ‘CSE02’, ‘CSE02’], ‘ques_date’ : [’10/11/2022′, ’06/06/2022′,’09/04/2022′, ’27/03/2022′, ’13/05/2010′, ’10/11/2021′,’11/1/2022′, ’27/02/2022′], ‘revenue’ : [77, 86, 55, 90, 65, 90, 80, 67]} ) df[‘ques_date’] = pd.to_datetime(df[‘ques_date’]) I would like… Read More Pandas – compute previous custom quarter wise total revenue and reshape table

Calculate 2 average values based on the same column, with different conditions

I have the following tables: Students (id, name, surname, study_year, department_id) Courses(id, name) Course_Signup(id, student_id, course_id, year) Grades(signup_id, grade_type, mark, date), where grade_type can be ‘e’ (exam), ‘l’ (lab) or ‘p’ (project) I want to display the average grade at the exam and the lab+project, for each student. SELECT new_table.id, new_table.name, new_table.grade_type, AVG(new_table.mark) AS "Average… Read More Calculate 2 average values based on the same column, with different conditions

Python: How to use value_counts() inside .agg function in pandas?

Input dataframe df looks like: item row Apple 12 Apple 12 Apple 13 Orange 13 Orange 14 Lemon 14 Output dataframe need to be item unique_row nunique_row count Apple {12,13} 2 {2,1} Orange {13,14} 2 {1,1} Lemon {14} 1 {1} Tried Code: df.groupby(‘item’, as_index=False)[‘row’].agg({‘unique_row’: lambda x: set(x) ,’nunique_row’: lambda x: len(set(x))}) So here, not sure… Read More Python: How to use value_counts() inside .agg function in pandas?

Get value with corresponding highest int with GROUP BY

I have 2 tables: quests (2 columns: player, quest) dimension_quest (2 columns: quest, order) quests dimension_quest For all players, I’d like to have the quest with the highest "order" column. I want all 3 columns (player, quest, order) Expected output Note that a quest has an unique "order" value in dimension_table. I cannot really aggregate… Read More Get value with corresponding highest int with GROUP BY

pivot a pandas dataframe

I have a dataframe df = pd.DataFrame({‘date’:[‘2021-06′,’2021-06′,’2021-09′,’2021-08′,’2021-09′],’type’:[‘t1′,’t1′,’t1′,’t2′,’t2’], ‘other_col’:[‘a’,’b’,’b’,’a’,’c’]}) and would like to pivot it such that I get the following output. date 2021-06 2021-08 2021-09 t1 count 2 0 1 mean 100% 0% 50% t2 count 0 1 1 mean 0% 100% 50% But I could not find out how to do it. >Solution :… Read More pivot a pandas dataframe