[英]Adding a value to a column in a DataFrame depending on a value in another column
I have a DataFrame with multiple columns.我有一个包含多列的 DataFrame。
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
现在我想将 append 值逐行添加到该列,如果值
index_1
index_1
lies between specific integer boarders.位于特定的 integer 寄宿生之间。
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.而 wf_tooold 是一个浮点数而 oldest_max 是一个整数。
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:用于初始化 dataframe 的代码示例:
d = {'index_1': [1,2,3,4,5,6,7,8,9,10,11,12]}
df = pd.DataFrame(data=d)
df["weighting_factor"]= ""
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:例如
oldest_max = 4
和wf_toold = 14.25
,我们得到:
>>> 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:然而,最好给
weighting_factor
一个NaN
作为起始值,否则 pandas 会将weighting_factor
视为Series
对象,而不是浮点数:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.