pandas-groupby

python pandas groupby/apply: what exactly is passed to the apply function?

python pandas groupby/apply: what exactly is passed to the apply function? Question: Python newbie here. I’m trying to understand how the pandas groupby and apply methods work. I found this simple example, which I paste below: import pandas as pd ipl_data = {‘Team’: [‘Riders’, ‘Riders’, ‘Devils’, ‘Devils’, ‘Kings’, ‘kings’, ‘Kings’, ‘Kings’, ‘Riders’, ‘Royals’, ‘Royals’, ‘Riders’], …

Total answers: 4

Pandas groupby throws: TypeError: unhashable type: 'numpy.ndarray'

Pandas groupby throws: TypeError: unhashable type: 'numpy.ndarray' Question: I have a dataframe as shown in the picture: problem dataframe: attdf I would like to group the data by Source class and Destination class, count the number of rows in each group and sum up Attention values. While trying to achieve that, I am unable to …

Total answers: 3

Aggregate daily data by month and an additional column

Aggregate daily data by month and an additional column Question: I’ve got a DataFrame storing daily-based data which is as below: Date Product Number Description Revenue 2010-01-04 4219-057 Product A 39.299999 2010-01-04 4219-056 Product A 39.520000 2010-01-04 4219-100 Product B 39.520000 2010-01-04 4219-056 Product A 39.520000 2010-01-05 4219-059 Product A 39.520000 2010-01-05 4219-056 Product A …

Total answers: 3

pandas groupby on columns with aggregation

pandas groupby on columns with aggregation Question: I have a dataframe like this: userId category count A cat 24 B dog 26 A cat 32 C bird 21 D lion 6 D cat 32 C bird 22 I want the result to be this. userId cat dog bird lion A 56 0 0 0 B …

Total answers: 1

Pandas GroupBy and select rows with the minimum value in a specific column

Pandas GroupBy and select rows with the minimum value in a specific column Question: I have a DataFrame with columns A, B, and C. For each value of A, I would like to select the row with the minimum value in column B. That is, from this: df = pd.DataFrame({‘A’: [1, 1, 1, 2, 2, …

Total answers: 8

Groupby class and count missing values in features

Groupby class and count missing values in features Question: I have a problem and I cannot find any solution in the web or documentation, even if I think that it is very trivial. What do I want to do? I have a dataframe like this CLASS FEATURE1 FEATURE2 FEATURE3 X A NaN NaN X NaN …

Total answers: 5

Aggregation in Pandas

Aggregation in Pandas Question: How can I perform aggregation with Pandas? No DataFrame after aggregation! What happened? How can I aggregate mainly strings columns (to lists, tuples, strings with separator)? How can I aggregate counts? How can I create a new column filled by aggregated values? I’ve seen these recurring questions asking about various faces …

Total answers: 2

Python Pandas: Calculate moving average within group

Python Pandas: Calculate moving average within group Question: I have a dataframe containing time series for 100 objects: object period value 1 1 24 1 2 67 … 1 1000 56 2 1 59 2 2 46 … 2 1000 64 3 1 54 … 100 1 451 100 2 153 … 100 1000 21 …

Total answers: 6

Use pandas.shift() within a group

Use pandas.shift() within a group Question: I have a dataframe with panel data, let’s say it’s time series for 100 different objects: object period value 1 1 24 1 2 67 … 1 1000 56 2 1 59 2 2 46 … 2 1000 64 3 1 54 … 100 1 451 100 2 153 …

Total answers: 2

How do I access a pandas groupby dataframe by grouped index?

How do I access a pandas groupby dataframe by grouped index? Question: Following this example I can create a simple dataframe and groupby import pandas as pd # Create a sample data frame df = pd.DataFrame({‘A’: [‘foo’, ‘foo’, ‘foo’, ‘bar’, ‘bar’], ‘B’: range(5), ‘C’: range(5)}) # group by ‘A’ and sum ‘B’ gf = df.groupby(‘A’).agg({‘B’: …

Total answers: 3