简体   繁体   English

根据另一列中的值向 DataFrame 中的列添加值

[英]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 = 4wf_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.

相关问题 根据列值是否在另一列中,将列添加到PySpark DataFrame - Adding column to PySpark DataFrame depending on whether column value is in another column dataframe 的列中存在的子集字符串,具体取决于另一列的值 - Pandas - Subsetting strings present in a column of a dataframe, depending on value of another column - Pandas 使用来自另一个 dataframe 的列的值填充列,具体取决于条件 - fill column with value of a column from another dataframe, depending on conditions 将 PySpark Dataframe 列转换为 Python 列表,具体取决于另一列中的值 - Convert a PySpark Dataframe Column to a Python List depending on the value in another column 根据另一列的值在 python 文件中添加一个额外的列 - Adding one extra column in a python file depending on the value of another column 我想在 dataframe 中迭代,在另一个 dataframe 中添加值(新列) - I want to iterate in dataframe adding value( new column) in another dataframe 如果值在另一个 dataframe 的另一列中,则添加列 - Add column if value is in another column of another dataframe 根据值是否为 null 创建 pandas dataframe 列 - Create a pandas dataframe column depending if a value is null or not 根据一列的值迭代 dataframe - Iterate a dataframe depending on the value of one column 直方图的颜色条取决于 dataframe 中的列值 - Colouring bars of histogram depending on column value in dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM