# Pandas-groupby Questions

Ad

## pandas groupby with length of lists

I need display in dataframe columns both the user_id and length of content_id which is a list object. but struggling to do using groupby. please

## Grouping columns, then deleting values based on null column values

I am reposting since my last question was poorly worded. i have a table that looks like the following:

## select a single value from a column after groupby another columns in python

I tried to select a single value of column class from each group of my dataframe after i performed the groupby function on the

## Generating a dataframe from a groupby transformation

I have this code: df.droupby('type)['feature1].mean() df has 15 features from 1 to

## Extract data from table column and make variables in Python

I have a dataset where i want to make a new variable everytime 'recording' number changes. i want the new variable to include the 'duration' data

## how to count the number of repetation of words and assign a number and append into dataframe

I am having a dataset of all the abstracts and the author gender. now i want to get the all the repetitions of words gender wise so that i can

## Pandas groupby with each group treated as a unique group

Please assist. how do i get the cumsum of a pandas groupby, but my data is boolean 0 and 1. i want to treat each group of 0s or 1s as unique

## make new dataframes from grouped dataframe automatically

I have this dataframe grouped df1 = pd.dataframe( { "name" : ["alice", "bob", "mallory", "mallory", "bob" , "mallory"] ,

## How to transform a dataframe for getting time of various occurence of events?

Given the following dataframe: +-------+-----+-------+-----+--------+---------------------------+ | did | cid | event | oid |

## how to make code faster by removing for loop

I am trying to do analysis of large amount of data , first i applied groupby function to divide data in different groups. then i check some

Ad

## Generate descriptive statistics for each row value and transpose dynamically

I have a dataframe like as shown below df = pd.dataframe({ 'subject_id':[1,1,1,1,2,2,2,2,3,3,4,4,4,4,4], 'readings' :

## Elegant way to get min and max using pandas

I have a dataframe like as shown below op1 = pd.dataframe({ 'subject_id':[1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2], 'date' :

## How to create pivot table with two different aggregations

I have a dataset on which i would like to run multiple aggregation steps using. this code creates the data: import pandas as pd

## GroupBy with ffill deletes group and does not put group in index

I'm running into a very strange issue ever since i have ported my code from one computer to another. i'm using pandas version 0.25.1 on this

## how to increment stage by condition in python pandas with groupby

I have data coming from two groups a and b. the task is to monitor change, and if change (leap) is bigger that 4, the stage is set higher by 1.

## How to add missing dates within date interval?

I have a dataframe like as shown below df = pd.dataframe({ 'subject_id':[1,1,1,1,1,1,1,2,2,2,2,2], 'time_1' :['2173-04-03

## how to keep running the loop in python even after a KeyError

I am having troubles with a code that is useful to create a pixel map, particularly in the loop that groups data in the selected area. i can't get

## Group By Month and Year in pandas dataframe

I have the below data set consisting of cards swiped and time when swiped. the output has to be total no of cards swiped month and year wise.

## python pandas groupby unexpected empty column

I want to aggregate some data to append to a dataframe. the following gives me the number of wins per name import pandas as pd

## Split CSV by unique columns

I ran into a problem trying to split my csv into the minimum value of csv files so each has only unique ids in it by running

## Create DataFrames based on unique values of a Column using Pandas then export to excel for each DF created

I have a set of data that needs to be filtered and saved in different excel based on unique values in a column. for example, in src

Ad

## How to create a new row on the fly by copying previous row

I have a dataframe like as given below edited dataframe df = pd.dataframe({

## ungrouping group by object by taking the first value in dataframe pandas

I have dataframe s which is a group by object: s = df.groupby(['x','y']) i would like to take the first event in

## Groupby on columns with overlapping groups

Continuing from my

## Get values by condition from different column and index

Given df that represent an events of users. index id action_id feature session_id n_page duration 1 1 null

## a complex computing of the average value in pandas

This is my first question on this forum. i am conducting experiments in which i measure the current-voltage curve of a device applying

## Groupby within groups

I have data like this: df = pd.dataframe({ 'a': ['milk', 'eggs', 'eggs', 'butter', 'butter', 'milk', 'eggs', 'eggs',

## Pandas Groupby - Calculate percentage of values per group total value

I have this pandas group by statement:

## Apply multiple conditional level groupby

Question 1: i have a data frame with two month value columns as month1 and month2. if the value

## Efficient and elegant way to fill values in pandas column based on each groups

Df_new = pd.dataframe( { 'person_id': [1, 1, 3, 3, 5, 5], 'obs_date': ['12/31/2007', 'na-na-na na:na:na', 'na-na-na na:na:na',

## How to create 5 new columns reflecting past 5 transactions of each customer?

Basically task is that for every customer last 5 transactions should show up but it should be on basis of that customer only. df =

## Shifting timeseries data using per group using shift() and groupby() results in NaN

Given the following dataset df = pd.dataframe( { 'yearmo': ['01', '02', '01', '02', '01', '02'], 'prod': ['a',

Ad

## Check if value exists in a column after groupby() and add the smallest value corresponding to it from another column

I have some data like this: df = pd.dataframe({'x':[1,2,3,1,1,2,3,3,2], 'y':['n', 'n', 'p', 'p', 'n', 'n', 'n',

## How to groupby a list?

For example, if we have a list of a lot of names. how to count occurrences in a sequence? or more exactly, how to use groupby to sort the list and

## How to use pandas Grouper with 7d frequency and fill missing days with 0?

I have the following sample dataset df = pd.dataframe({ 'names': ['joe', 'joe', 'joe'], 'dates': [dt.datetime(2019,6,1),

## Length of a group within a group (apply groupby after a groupby)

I am facing the next problem: i have groups (by id) and for all of those groups i need to apply the following code: if the distances between

## Efficient way to use pandas group by on a million records

I have a dataframe which can be generated using the code below df2 = pd.dataframe({'subject_id':[1,1,1,1,1,1,2,2,2,2],'colum' :

## Pandas - How to convert row data to columns

I want to groupby my data using a column (no) and keep each result of the columns date1 and

## Pandas/Numpy Groupby + Aggregate (inc integer mean) + Filter

I'm new to pandas/numpy and i'm playing around to see how everything works. i'm using this dataset for the top 1000 imdb movie ratings:

## Pandas: how to groupby based on series pattern

Having the following df: pd.dataframe({'bool':[true,true,true, false,true,true,true], 'foo':[1,3,2,6,2,4,7]})

## Which Pandas function do I need? group_by or pivot

I'm still relatively new to pandas and i can't tell which of the functions i'm best off using to get to my answer. i have looked at pivot,

## Plot graph of difference in one column for grouped columns of pandas dataframe

I have a dataframe, a simplified example is below: cycle sensor value 0 0 1 0.34 1 0 1 0.80 2

## Faster nested for-loop, perhaps using pd.groupby

I have a nested loop. but my data set is very large, so i need a faster way. i believe it can be done with grouping or mapping the data in some

Ad

## Proportional/Percentage values

I have this data frame: o d r kz p 1 3 1 5 nan 1 3 2 0 nan 1 10 1 7 nan 1 10 3 1 nan 1 10

## Extend data/shrink window for pct_change at end of series

I am trying to calculate percent change (for periods greater than 1) with a shrinking window effect at the end of a series. the following

## Pandas Group By, Aggregate, Then Return A Different Column

I have a pandas dataframe containing baseball fielding statistics. each row shows how many games a player has appeared at a given position over

## "No numeric types to aggregate" while using Pandas expanding()

In pandas 1.1.4, i am receiving a dataerror: no numeric types to aggregate when using an expandinggroupby. example dataset:

## Collapse rows of a dataframe with common values and fill in blanks

I have a single data frame and every row is duplicated except for two values. in all cases the corresponding duplicate has a blank value in the

## Get count of each unique values groupby another column and transform them into columns

I have a dataframe like below: id name cola

## Filter out dataframe rows within groups that are not an exact multiple of a previous year

I'd like to filter out columns within each group of 'ticker' so that what remains are only rows exactly 1, 2, 3, etc. years before my most recent

## How to groupby the keys in dictionary and sum up the values in python?

How to groupby two keys in dictionary and get the sum of the values of the other key val.

## How to find Non repeat Occurrence of column with respect to another column

>>> df=pd.dataframe({'order no':[71,71,71,71,71,71,71,72,72,72,72,72,72,72,73,73],'product

## Pandas Groupby Plotting MultiIndex Grouped by Top Level

I am struggling to produce a pandas groupby multiindex plot the way i want. i have the following dummy pandas dataframe: data = {

## keep second level of multi-index intact while sorting on first one pandas python

I have sorted my first level of index using the following method :

Ad

## create a matrix with two dataframe - pandas?

I have two data, one with columns: df1 = id as hs ts a a_1 a_6 a_7

## How to get the minimum values in a quasi periodic series?

For the series s t = [0.1, 1, 2, 3, 0, 1, 2, 3, 4, 5, 0.9, 1, 2] s = pd.series(t) i would like to get the

## Combine index and columns and keep value

I have a dataframe as below a b c 1 1 2 3 2 4 2 5 and i want to combine index and

## Pandas datetime week not as expected

When working with pandas datetimes, i'm trying to group data by the week and year. however, i have noticed some years where the last day of the

## Dataframe groupby - list of values

I have a following dataframe: driver_id status dttm 9f8f9bf3ee8f4874873288c246bd2d05 free

## What are the built-in methods for DataFrameGroupBy.transform?

Dataframegroupby objects are output by dataframe.groupby and have the method transform. according to the

## Add column with number of ratings per user, pandas

I am working with a book rating dataset of the form userid | isbn | rating 23413 1232 2.5 12321 2311 3.2 23413

## Is there a Python library to group values of Col B based on Col A and display all values of a group in a single row?

I want the following data to be converted like the below expected output. values of 2nd column must be grouped and displayed in a single row based

## Slicing dataframe into new dataframes

I have to slice my dataframe into new dataframes, grouped by the destination (i'm using pandas). this is my dataframe called

## Access specific group in group by operation with pandas

a b 0 1 12 1 1 13 2 1 15 3 2 16 4 2 19 5 2 20 6 3 32 7 3 29 8 3 25 9 4 3 10 4 5 11 4 7 i

## deleting groups based on a condition from a dataframe - pandas groupby

This is my dataframe: df = pd.dataframe({'sym': list('aaaaaabb'), 'order': [0, 0, 1, 1, 0, 1, 0, 1], 'key': [2, 2, 2, 2, 3, 3, 4,

Ad

## How to create a 2 level groupby of top n items

I have this dataframe state county pop 1 alabama autauga county 54571 2

## Get Max aggregations for multiple columns in pandas groupby object

I have a dataframe and want to groupby one column, "company" and aggregate multiple columns and find the company with the max value for each

## How to calculate running sum based on ID and date

I have a dataset in which i have the following columns: date, id, value. i then want a running sum of the preceding 3 days (including the current

## Merging CSV entries by date and counting the entries per date

I have a csv file that i'm using pandas dataframe to manipulate. the data that i have is tweet data, and what i'm trying to do is merge the cells

## Getting the nlargest of each group in a Multiindex Pandas Series

I have a dataframe that consists of information about every nfl play that has occurred since 2009. my goal is to find out which teams had the most

## df.groupby() - how to aggregate data where order of grouped data is important?

How can i aggregate data when the order of grouped data is important? (bonus points if this can be done in an elegant vectorized

## Replace min by a quantile in a transform after groupby

I am calculating the minimum of each group using this piece of code: df['columnname'].groupby(group_identifier).transform('min')

## Pandas - How to perform OLS Regression of values versus time in each group of a dataframe?

I have hourly readings in a dataframe of the form: date_time temp 2001-01-01 00:00:00 -1.3 2001-01-01

## Looking at Previous Time series

I have a dataset as shown below. the idea is looking at every previous 15minutes not the frequency which we use in grouper function. i want to see

## python access a column after groupby

I would like to replace null value of stadium attendance (affluence in french) with their means. therefore i do this to have the mean by seasons /

## Puzzled over the behavior of Pandas in groupby

I have a large dataset which has among others a binary variable: transactions['has_acc_id_and_cus_id'].value_counts() 1 1295130

Ad

## How to mask values in column based on a condition per group

I have pandas dataframe like this: data = {'id_1':['a', 'a','a', 'b', 'b', 'b'], 'id_2':[1, 2, 2, 1, 1, 2],

## Using groupby and append values at columns

Consider the following csv file where there is a duplicate name in "name" column: id,name,t,ca,i,c,ip

## How to use groupby and cumcount on unique names in a Pandas column

I have a dataframe that looks like this id ..... config_name config_version ... aa a 0

## Pandas : data frame transformation

I have a pandas dataframe which looks like

## How to sort unique table in dataframe based on a single column?

Have df with values 0 | 1 | 2 0 sun | east | pass 1 moon | west | pass 2 mars | north |

## Set value when row is maximum in group by - Python Pandas

I am trying to create a column (is_max) that has either 1 if a column b is the maximum in a group of values of column a or 0 if it is not.

## Pandas groupby with bin counts for timeseries

On a sample dataframe data = pd.dataframe(np.random.rand(6,2), columns = list('ab')) dti = pd.date_range(start='2019-02-12',

## Pandas source code import multiple modules

I was looking at the pandas source code

## Averaging the last few entries of every unique value in a column to generate new df

I have a df.head() of my data frame looks like this. i'm measuring my data somewhere between 7 and 9 hz frequency and have about 100

## Increasing count where a condition is met within pandas GroupBy

How do i count the number of multicolumn (thing, cond=1) event occurrences prior to every (thing, cond=any)

## pandas GroupBy plotting each group

I have some data from which i want to extract a time series of revenues (sum of dollars in different dates day over

Ad

## Pandas Sum Diagonal Value with groupby

I would like to sum the diagonal value of each year and residue, grouping by object. for example for object a will be 1 + 10 + 11 + 12 + 13. is

## Reform pandas dataframe

I have a dataframe: df1 = pandas.dataframe( { "text" : ["alice is in ", "alice is in

## Panda Group by time and count value of column

Let say i have an array with event and log time, like this: time event 01/01/2019 8h00 x 01/01/2019 8h10 y 01/01/2019

## Left join in pandas without the creation of left and right variables

I'm missing something in the syntax of merging in pandas. i have the following 2 data frames: >>> dfa s_name

## IndexError when replacing missing values with mode using groupby in pandas

I have a dataset which requires missing value treatment. column missing values complaint_id

## Finding the top categories of a column value based on the other column

Finding the top categories of a column value based on the other column df : nationality age card category amount india

## how to group by and sort the columns given the column value in a function

I have a data frame as below, i need to write a function which should be able to give me the below results: input parameters:

## Dropping duplicates within groups only

I want to drop duplicates only in particular subsets from a data frame. under each "spec" in the column "a" i want to drop duplicates, but i want

## Pandas groupby and get dict in list

I'm trying to extract grouped row data to use values to plot it with label colors another file. my dataframe is like below.

## Determine size within each each group having the same value in another column

I have dataframe like so, id,class_id,active 1,123,0 2,123,0 3,456,1 4,123,0

## Display the fruits bought by particular student with respective price

There are different names who have bought different fruits. i need the person name and the different fruits he has bought with the price

Ad

## Convert categories in columns into multiple columns coded as 1 or 0 based on the unique key in Python

I have data like this: user reg ind prod a asia tele tv a asia bank phone a

## Filter within GroupBy objects in Pandas

Here's a sample dataframe: import pandas as pd df = pd.dataframe({'id':[1,1,1,2,2,2,3,3], 'value':[42, 89,

Ad

## Blog Categories

Ad