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:
Column_A --> Time constant = 5 secs Column_B --> Time constant = 15 secs Column_C --> Time constant = 60 secs Column_D --> Time constant = 200 secs
Any ideas?
Thanks!
Advertisement
Answer
You can use apply
with the args
passed as a tuple like so:
def your_filter_func(x, gain, time_constant): return some_filter(x, gain, time_constant) df = df.apply(your_filter_func, args=(gain, time_constant), axis=0)
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:
def your_filter_func2(column, gains, time_constants): return some_filter(x, gains[column.index], time_constants[column.index]) df = df.apply(your_filter_func2, args=(gains, time_constants), axis=0)
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.