简体   繁体   中英

calculating median using rolling window in pandas across multiple rows and columns

Have a dataset with 5 rows and 3 columns. Index is resampled at 2 seconds. So I want to calculate the rolling median at frequency =2.

so while calculating the median at frequency 2 it should look for first 2 rows and all 3 columns like B, c and d to come up with a median. Similarly the second window should consider both rows and all 3 columns B ,c and d to come up with the median.

df = pd.DataFrame({'B': [0, 1, 2, np.nan, 4],'c': [2,4,7,8,9],'d': [2,8,7,5,9]},
               index = [pd.Timestamp('20130101 09:00:00'),
                        pd.Timestamp('20130101 09:00:02'),
                        pd.Timestamp('20130101 09:00:03'),
                        pd.Timestamp('20130101 09:00:05'),
                        pd.Timestamp('20130101 09:00:06')])

In pandas we can pass only one column in rolling window , how to pass multiple columns. so that it looks for all the elements present in that rows amongst all columns.

df.B.rolling('2s').median()

I expected the output to be :

2013-01-01 09:00:00     Nan

2013-01-01 09:00:02     1.0

2013-01-01 09:00:03      5.5

and so on

how can we pass multiple columns in the pandas rolling function

Just don't use a column reference

df.rolling('2s').median()

                         B       c       d
2013-01-01 09:00:00     0.0     2.0     2.0
2013-01-01 09:00:02     1.0     4.0     8.0
2013-01-01 09:00:03     1.5     5.5     7.5
2013-01-01 09:00:05     NaN     8.0     5.0
2013-01-01 09:00:06     4.0     8.5     7.0

Note if you calculate the median for just 2 elements it returns the same result as the mean would. This is because there cannot be any median calculated for 2 elements since it is defined as the element that seperates a list of elements in higher and lower values.

df.rolling('2s').mean()

                          B      c       d
2013-01-01 09:00:00     0.0     2.0     2.0
2013-01-01 09:00:02     1.0     4.0     8.0
2013-01-01 09:00:03     1.5     5.5     7.5
2013-01-01 09:00:05     NaN     8.0     5.0
2013-01-01 09:00:06     4.0     8.5     7.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM