Skip to content
Advertisement

How to apply a first order filter with a different time constant for each column of a pandas dataframe?

everyone! I have a pandas dataframe where each column represents a noisy signal, and I would like to apply a first order filter (gain = 1, time constant = “x” seconds) in each column, but with a different time constant for each column. For example:

JavaScript

Any ideas?

Thanks!

Advertisement

Answer

You can use apply with the args passed as a tuple like so:

JavaScript

This applies your_filter_func to each column (that’s the axis=0 part).

If you also want to apply a separate gain and time_constant to each column, you can do this by using the index of the column and adjusting accordingly:

JavaScript

And just make sure that the gains and time_constants are an iterable (e.g. list or numpy array) where you can index into it by the index of the column, to pull out the specific gain and time_constant for that column.

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