Skip to content
Advertisement

Adding a value to a column in a DataFrame depending on a value in another column

I have a DataFrame with multiple columns.

JavaScript

The column

weighting_factor

is empty. Now I want to append values to that column row by row, if the value of

index_1

lies between specific integer boarders.

I tried

JavaScript

whereas wf_tooold is a float and oldest_max is an int.

The error that I get is

JavaScript

What would be a good way to fill in the value in the corresponding column?

Code sample to initialize a dataframe:

JavaScript

Advertisement

Answer

You basically want to update a filtered number of rows with a value, so you do that with:

JavaScript

for example with oldest_max = 4 and wf_toold = 14.25, we get:

JavaScript

It might however be better to give weighting_factor a NaN as starting value, otherwise pandas will see the weighting_factor as a Series of objects, not floats:

JavaScript

you can check between a lower bound and an upperbound with:

JavaScript
Advertisement