Home > Back-end >  How to add new column based on the condition of another column in pandas?
How to add new column based on the condition of another column in pandas?

Time:09-28

I have a dataframe that consists some variables such as country, revenue, cost, and I have a constant value.

I want to create new column based on some range I defined and I tried this:

my_df['flag'] = my_df.apply(lambda row: row.cost - constant < row.revenue < row.cost   constant, 1,0)

It gave me a result but the flag column is full of 'False' value. What is the problem?

CodePudding user response:

Use Series.between with numpy.where:

my_df['flag'] = np.where(my_df.revenue.between(my_df.cost - constant, 
                                               my_df.cost   constant,
                                               inclusive='neither'), 1, 0)

Or:

my_df['flag'] = my_df.revenue.between(my_df.cost - constant, 
                                      my_df.cost   constant,
                                      inclusive='neither').astype(int)
  • Related