Thursday, November 3, 2022

[FIXED] How to add new column based on the condition of another column in pandas?

Issue

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?


Solution

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)


Answered By - jezrael
Answer Checked By - Mildred Charles (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.