Home > Back-end >  How to bin data on the basis of positive, negative or zero values?
How to bin data on the basis of positive, negative or zero values?

Time:05-02

new_df = pd.concat(new_dataset)
print(new_df.shape)
new_df = new_df.dropna(how='any') 
print(new_df.shape)
new_df.head(20)

enter image description here

Now I want to add another column naming 'Close_gap_IUD' on the basis of values from 'Close_gap' column. I want:

  1. 'Increase' label in 'Close_gap_IUD' column, for the positive values(>0) of 'Close_gap' column, 2. 'Decrease' label in 'Close_gap_IUD' column, for the negative values(<0) of 'Close_gap' column,
  2. 'Unchanged' label in 'Close_gap_IUD' column, for the zero values(==0) of 'Close_gap' column

How can I do this?

CodePudding user response:

new_df["Close_gap_IUD"] = new_df["Close_gap"].apply(lambda x: "Increase" if x > 0 else "Decrease" if x < 0 else "Unchanged")
  • Related