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

Friday, November 4, 2022

[FIXED] How to apply two different functions to one column if meets the condition?

 November 04, 2022     lambda, pandas, python     No comments   

Issue

I need to apply 2 different functions to 1 columns if it meets a condition. My dataframe looks like this. I want to apply the functions to the column produce: veg_pro if the category is a vegetable and fruit_pro if it's a fruit.

Produce            Category
apple is good      fruit
corn is bad        vegetable
beans is good      vegetable
grape if good      fruit

My functions look like this:

def veg_pro(text):
    reg_tokenizer = RegexpTokenizer('\s+', gaps = True)
    terms = reg_tokenizer.tokenize(text) 
    return terms

def fruit_pro(text):
    reg_tokenizer = RegexpTokenizer(“[\w+.]+“)
    terms = reg_tokenizer.tokenize(text) 
    return terms

 df['produce']= df['produce'].apply(lambda x: 
 veg_pro(x) if df['Category'] =='vegetable’ else 
 fruit_pro(x))
    
 ValueError: The truth value of a Series is ambiguous. Use 
 a.empty, a.bool(), a.item(), a.any() or a.all().

Solution

instead of using apply on one column use it on the dataframe like this:

 df['produce']= df.apply(lambda x: 
 veg_pro(x["produce"]) if x["Category"] =="vegetable" else fruit_pro(x["produce"]),axis=1)


Answered By - to_data
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • 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