Skip to content
Advertisement

Pandas DataFrame mean of data in columns occurring before certain date time

I have a dataframe with ID’s of clients and their expenses for 2014-2018. What I want is to have the mean of the expenses per ID but only the years before a certain date can be taken into account when calculating the mean value (so column ‘Date’ dictates which columns can be taken into account for the mean).

Example: for index 0 (ID: 12), the date states ‘2016-03-08’, then the mean should be taken from the columns ‘y_2014’ and ‘y_2015’, so then for this index, the mean is 111.0. If the date is too early (e.g. somewhere in 2014 or earlier in this case), then NaN should be returned (see index 6 and 9).

Desired output:

JavaScript

The code below is what I tried.

Tried code:

JavaScript

Advertisement

Answer

Solved: one possible answer to my own question

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement