group-by

pandas combine specific excel sheets into one

pandas combine specific excel sheets into one Question: I have an excel sheet named output.xlsx with multiple sheets in it. Example, the sheets within it are named as P1,P2,P3,P4 I would like to do the below a) Combine sheet P1 and sheet P2 into one single sheet name it as P1&P2 b) retain the P3 …

Total answers: 1

pandas groupby, split df and rename multiple sheets

pandas groupby, split df and rename multiple sheets Question: I have a dataframe like as below import numpy as np import pandas as pd from numpy.random import default_rng rng = default_rng(100) cdf = pd.DataFrame({‘Id’:[1,2,3,4,5], ‘customer’: rng.choice(list(‘ACD’),size=(5)), ‘segment’: rng.choice(list(‘PQRS’),size=(5)), ‘manager’: rng.choice(list(‘QWER’),size=(5)), ‘dumma’: rng.choice((1234),size=(5)), ‘damma’: rng.choice((1234),size=(5)) }) I would like to do the below a) create an …

Total answers: 1

Assigning weight to a column using after pandas groupby

Assigning weight to a column using after pandas groupby Question: I would Like to add weight to a groupby dataframe: for example I have: df = pd.DataFrame({‘Account’: [1, 2, 3, 1, 2, 3, 3], ‘Money’: [4, 5, 6, 8, 9, 10, 11]}) df Account Money 0 1 4 1 2 5 2 3 6 3 …

Total answers: 1

Pandas groupby columns and multiply two other columns in the aggregate function

Pandas groupby columns and multiply two other columns in the aggregate function Question: I have a hopefully easy problem for some help stack helpers! I have a dataframe: df = pd.DataFrame({‘Quantity’: [2, 3, 4, 1, 2, 1, 4, 5], ‘User’: [‘A’, ‘A’, ‘B’, ‘B’, ‘B’, ‘C’, ‘C’, ‘C’], ‘Price’: [5, 3, 2, 6, 2, 3, …

Total answers: 3

Python inequality join with group by

Python inequality join with group by Question: I have the following two dataframes import pandas as pd dates = [’31-12-2015′, ’31-12-2016′, ’31-12-2017′, ’31-12-2018′] df1 = pd.DataFrame({‘id’: [1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4], ‘t’: dates*4, ‘stage’: [1,2,2,3,1,1,2,3,1,1,1,3,2,1,1,3]}) df2 = df1.loc[df1[‘stage’] == 1] What is the most efficient way of doing the operation below in python? Select a.id ,a.t ,max(b.stage = 2) …

Total answers: 1

Reorganizing DataFrame (interchanging row information as columns)

Reorganizing DataFrame (interchanging row information as columns) Question: I have the next DataFrame. data={ ‘date’:[‘2023-01’, ‘2023-01’, ‘2023-01’, ‘2023-01’, ‘2023-02’, ‘2023-02’, ‘2023-02’, ‘2023-02’, ‘2023-02’], ‘concept’:[‘FOOD’, ‘CAR’, ‘EDUCATION’, ‘ELECTRICITY’, ‘FOOD’, ‘CAR’, ‘TRAVEL’, ‘WATER’, ‘ELECTRICITY’], ‘amount’:[455.54, 237.41, 3.91, 213.2, 28.72, 422.72, 263.29, 3.93, 89.35] } df = pd.DataFrame(data) I would like to reorder the information so I have …

Total answers: 1

Pandas: calculate time difference between different milestones in column

Pandas: calculate time difference between different milestones in column Question: I have a table like this: id tm milestone 00335c06f96a21e4089c49a5da 2023-02-01 18:13:42.307543 A 00335c06f96a21e4089c49a5da 2023-02-01 18:14:42.307543 A 00335c06f96a21e4089c49a5da 2023-02-01 18:15:42.307543 A 00335c06f96a21e4089c49a5da 2023-02-01 18:19:10.307543 B 00335c06f96a21e4089c49a5da 2023-02-01 18:21:05.307543 C 0043545f6b9112c7e471d5cc81 2023-02-02 08:06:42.307543 A 0043545f6b9112c7e471d5cc81 2023-02-02 08:07:42.307543 A 0043545f6b9112c7e471d5cc81 2023-02-02 09:05:42.307543 B 0043545f6b9112c7e471d5cc81 2023-02-02 09:05:42.307543 B …

Total answers: 1

Grouping by Aggregate Functions in #Pandas

Grouping by Aggregate Functions in #Pandas Question: I’m trying to find out which occupation has the max mean salary. I’ve tried df.groupby(‘Occupation’).agg({‘Salary’:’mean’}) I think I’ve figured out how to get the max mean salary but I can’t figure out how to get the specific occupation title. Any tips ? Thank you!! Asked By: kristinek || …

Total answers: 1

Group a DataFrame by months and an additional column

Group a DataFrame by months and an additional column Question: I have the next DataFrame: data={ ‘date’:[’02/01/2023′, ’03/01/2023′, ’12/01/2023′, ’16/01/2023′, ’23/01/2023′, ’03/02/2023′, ’14/02/2023′, ’17/02/2023′, ’17/02/2023′, ’20/02/2023′], ‘amount’:[-2.6, -230.0, -9.32, -13.99, -12.99, -50.0, -5.84, -6.6, -11.95, -20.4], ‘concept’:[‘FOOD’, ‘REPAIR’, ‘HEALTH’, ‘NO CLASSIFIED’, ‘NO CLASSIFIED’, ‘REPAIR’, ‘FOOD’, ‘NO CLASSIFIED’, ‘FOOD’, ‘HEALTH’] } df = pd.DataFrame(data) I need …

Total answers: 1