Skip to content
Advertisement

Tag: pandas-groupby

How do I find first and last value of each day in pandas dataframe

I have a pandas DataFrame like the below: Price Date 25149.570 2/5/2017 14:22 24799.680 2/5/2017 14:22 24799.680 2/5/2017 14:22 14570.000 2/5/2017 14:47 14570.001 2/5/2017 14:47 14570.001 2/5/2017 14:47 14570.000 2/5/2017 15:01 14570.001 2/5/2017 15:01 14570.001 2/5/2017 15:01 14600.000 2/6/2017 17:49 14600.000 2/6/2017 17:49 14800.000 2/6/2017 17:49 14600.000 2/6/2017 17:49 14600.000 2/6/2017 17:49 14600.000 2/6/2017 18:30 14600.000 2/6/2017 18:30 14800.000 2/6/2017

Pandas: using groupby to calculate a ratio by specific values

Hi I have a dataframe that looks like this: and I want to calculate a ratio in the column ‘count_number’, based on the values in the column ‘tone’ by this formula: [‘blue’+’grey’]/’red’ per each unite combination of ‘participant_id’, ‘session’, ‘block’ – here is part of my dataset as text, the left column ‘RATIO’ is my expected output: participant_id session block

Pandas groupby counting values > 0

I have a pandas df of the following format I am looking to transform it such that I land up with the below result Essentially for “HIGH_COUNT” and “LOW_COUNT” I want to count the number of occurrences that column was greater than 0, grouped by “MATERIAL”. I have tried to do df.groupby([‘MATERIAL’]).agg<xxx> but I am unsure of the agg function

Advertisement