简体   繁体   English

基于另一列中相应行的条件值填充数据框的列的最佳方法是什么?

[英]What is the best way to populate a column of a dataframe with conditional values based on corresponding rows in another column?

I have a dataframe, df, in which I am attempting to fill in values within the empty "Set" column, depending on a condition. 我有一个数据框df,其中我尝试根据条件在空的“设置”列中填充值。 The condition is as follows: the value of the 'Set' columns need to be "IN" whenever the 'valence_median_split' column's value is 'Low_Valence' within the corresponding row, and "OUT' in all other cases. 条件如下:只要相应行中的“ valence_median_split”列的值为“ Low_Valence”,“设置”列的值就必须为“ IN”,而在所有其他情况下,则需要为“ OUT”。

Please see below for an example of my attempt to solve this: 请参阅以下示例,以尝试解决此问题:

df.head()

Out[65]: 
              ID Category  Num Vert_Horizon Description  Fem_Valence_Mean  \
0  Animals_001_h  Animals    1            h  Dead Stork              2.40   
1  Animals_002_v  Animals    2            v        Lion              6.31   
2  Animals_003_h  Animals    3            h       Snake              5.14   
3  Animals_004_v  Animals    4            v        Wolf              4.55   
4  Animals_005_h  Animals    5            h         Bat              5.29   

   Fem_Valence_SD  Fem_Av/Ap_Mean  Fem_Av/Ap_SD  Arousal_Mean ...   Contrast  \
0            1.30            3.03          1.47          6.72 ...      68.45   
1            2.19            5.96          2.24          6.69 ...      32.34   
2            1.19            5.14          1.75          5.34 ...      59.92   
3            1.87            4.82          2.27          6.84 ...      75.10   
4            1.56            4.61          1.81          5.50 ...      59.77   

   JPEG_size80   LABL   LABA   LABB  Entropy  Classification  \
0       263028  51.75  -0.39  16.93     7.86                   
1       250208  52.39  10.63  30.30     6.71                   
2       190887  55.45   0.25   4.41     7.83                   
3       282350  49.84   3.82   1.36     7.69                   
4       329325  54.26  -0.34  -0.95     7.82                   

   valence_median_split  temp_selection  set  
0           Low_Valence   Animals_001_h       
1          High_Valence             NaN       
2           Low_Valence   Animals_003_h       
3           Low_Valence   Animals_004_v       
4           Low_Valence   Animals_005_h       

[5 rows x 36 columns]

df['set'] = np.where(df.loc[df['valence_median_split'] == 'Low_Valence'], 'IN', 'OUT') 

ValueError: Length of values does not match length of index

I can accomplish this by using loc to separate the df into two different df's, but wondering if there is a more elegant solution using the "np.where" or a similar approach. 我可以通过使用loc将df分为两个不同的df来完成此操作,但是想知道是否存在使用“ np.where”或类似方法的更优雅的解决方案。

Change to 改成

df['set'] = np.where(df['valence_median_split'] == 'Low_Valence', 'IN', 'OUT') 

If need .loc 如果需要.loc

df.loc[df['valence_median_split'] == 'Low_Valence','set']='IN'
df.loc[df['valence_median_split'] != 'Low_Valence','set']='OUT'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据另一个 dataframe 的列条目过滤一个 dataframe 的行的最佳方法是什么 - What is the best way to filter rows of one dataframe based on column entries of another dataframe 访问数据框列中值的最佳方法是什么? - What is the best way to access values in a dataframe column? 根据另一个 dataframe 的匹配行和列填充 dataframe 中的值 - Populate values in a dataframe based on matching row and column of another dataframe 有没有一种方法可以根据与Pandas中另一列关联的值来填充列? - Is there a way to populate a column based on values associated with another column in Pandas? 获取包含一列中的值与另一个数据框中的对应列中的值相近的数据框行 - get dataframe rows that contain values in a column that are close to those in a corresponding column in another dataframe 在 python 中创建新的数据框列和填充值的有效方法是什么? - What is an efficient way to create new dataframe column and populate values in python? 根据特定月份的值过滤熊猫数据框,并以另一列为条件 - Filter a pandas Dataframe based on specific month values and conditional on another column 列中的条件格式单元格基于它在另一列中的对应值 - Conditional format cell in column based on it corresponding value in another column Pandas - 根据另一个填充一个数据框列 - Pandas - populate one dataframe column based on another 使用来自另一个数据框的相应数据填充列值(合并??) - Populate column value with corresponding data from another dataframe (merge??)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM