PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, November 3, 2022

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

 November 03, 2022     apply, lambda, pandas, python     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing