aggregate-functions

Alternative of pd.NamedAgg to a code compliant with pandas 0.24.2?

Alternative of pd.NamedAgg to a code compliant with pandas 0.24.2? Question: When I’m trying to run my code, I’m facing the error: AttributeError: module ‘pandas’ has no attribute ‘NamedAgg’ . Can someone help me with the alternative of this?. Thanks My code: import pandas as pd import re df = pd.read_excel(‘testingfile.xlsx’) df_grouped = df.groupby([‘result_by’, ‘variable’]).agg(count_col=pd.NamedAgg(column=’variable’, …

Total answers: 1

pivot a pandas dataframe

pivot a pandas dataframe Question: 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 …

Total answers: 3

How to get rid of nested column names in Pandas from group by aggregation?

How to get rid of nested column names in Pandas from group by aggregation? Question: I have the following code that finds the total and unique sales for each employee using a group by with Employee_id and aggregation with Customer_id. Sales.groupby(‘Employee_id’).agg({ ‘Customer_id’: [ (‘total_sales’, ‘count’), (‘unique_sales’, ‘nunique’) ]}) It is important to know that I …

Total answers: 1

Naming returned columns in Pandas aggregate function?

Naming returned columns in Pandas aggregate function? Question: I’m having trouble with Pandas’ groupby functionality. I’ve read the documentation, but I can’t see to figure out how to apply aggregate functions to multiple columns and have custom names for those columns. This comes very close, but the data structure returned has nested column headings: data.groupby(“Country”).agg( …

Total answers: 6

Group/Count list of dictionaries based on value

Group/Count list of dictionaries based on value Question: I’ve got a list of Tokens which looks something like: [{ Value: “Blah”, StartOffset: 0, EndOffset: 4 }, … ] What I want to do is get a count of how many times each value occurs in the list of tokens. In VB.Net I’d do something like… …

Total answers: 5

Apply multiple functions to multiple groupby columns

Apply multiple functions to multiple groupby columns Question: The docs show how to apply multiple functions on a groupby object at a time using a dict with the output column names as the keys: In [563]: grouped[‘D’].agg({‘result1’ : np.sum, …..: ‘result2’ : np.mean}) …..: Out[563]: result2 result1 A bar -0.579846 -1.739537 foo -0.280588 -1.402938 However, …

Total answers: 8