2

I have a DataFrame with multiple columns.

    base_rate weighting_factor  index_1  
0         NaN                         0  
1    1.794836                         1  
2    1.792804                         2  
3    1.795893                         3  
4    1.798023                         4  
5    1.795517                         5  
6    1.798652                         6  
7    1.794425                         7  
8    1.796899                         8 

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

if df['index1'] <= oldest_max:
    werte_df["weighting_factor"].append(wf_tooold)

whereas wf_tooold is a float and oldest_max is an int.

The error that I get is

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

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

Code sample to initialize a dataframe:

d = {'index_1': [1,2,3,4,5,6,7,8,9,10,11,12]}
df = pd.DataFrame(data=d)
df["weighting_factor"]= ""
2
  • Plesse include code to initialize your dataframe. Commented Aug 22, 2021 at 20:10
  • 1
    I updated the question, hope this fits your needs Commented Aug 22, 2021 at 20:19

1 Answer 1

1

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

df.loc[df['index_1'] <= oldest_max, 'weighting_factor'] = wf_toold

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

>>> df
    index_1 weighting_factor
0         1            14.25
1         2            14.25
2         3            14.25
3         4            14.25
4         5                 
5         6                 
6         7                 
7         8                 
8         9                 
9        10                 
10       11                 
11       12

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:

from numpy import NaN
df['weighting_factor']= NaN

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

df.loc[df['index_1'].between(old_min, oldest_max), 'weighting_factor'] = wf_toold
Sign up to request clarification or add additional context in comments.

2 Comments

really helpfull, thank you! And if I now want to update the rows which are bigger than oldest max ( >= oldest_max) but below old_min (<=)? I get an error if I try df.loc[df['index_1'] >= oldest_max & <= old_min, 'weighting_factor'] = wf_old
@maki: you should work with df.loc[(df['index_1'] >= oldest_max) & (df['index_1'] <= old_min), 'weighting_factor'] = wf_old, or better df.loc[(df['index_1'].between(old_min, oldest_max), 'weighting_factor'] = wf_old

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.