Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

python - Creating a new column based on the values of other columns

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...