根据其他列的值创建新列 [英] Creating a new column based on the values of other columns
本文介绍了根据其他列的值创建新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个高价值指标"列,根据两个不同的值列显示Y"或N".当 Value_1 > 1,000 或 Value_2 > 15,000 时,我希望新列有一个Y".下面是表格,所需的输出将包括基于或条件的指标列.
I wanted to create a "High Value Indicator" column, which says "Y" or "N" based on two different value columns. I want the new column to have a "Y" when Value_1 is > 1,000 or Value_2 > 15,000. Bellow is the table, the desired output would include the indicator column based on the or condition about.
ID Value_1 Value_2
1 100 2500
2 250 6250
3 625 15625
4 1500 37500
5 3750 93750
推荐答案
尝试使用 .loc 和 .fillna
Try using .loc and .fillna
df.loc[((df['Value_1'] > 1000)
|(df['Value_2'] > 15000)), 'High_Value_Ind'] = 'Y'
df['High_Value_Ind'] = df['High_Value_Ind'].fillna('N')
这篇关于根据其他列的值创建新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文