'How to bin data on the basis of positive, negative or zero values?

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?



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MoRe