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"]= ""
Advertisement
Answer
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