-1

There are two columns in my dataframe. One is called brand name,another is total amount enter image description here

I defined a function to determine that:

if the brand name == 'something' and total amount > 'some number'
then return 'something'

I want to apply the function to dataframe - df[["Brand Name","Total amount"]] and it return errors.

Please advise how to solve this problem?Thanks.

Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
kai alex
  • 53
  • 5
  • 2
    Please don't use image as input example.https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – Hsgao Nov 01 '18 at 06:28
  • 1
    Can you show your function for determining `something` and `total amount > 'some number'`?. Also, please provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – Space Impact Nov 01 '18 at 06:30
  • What errors are you getting? How did you define the function? How did you try to apply it to those two columns? – r2evans Nov 01 '18 at 07:49

2 Answers2

3

You actually don't need a function. pandas provides you options to filter the dataset:

df[(df[brand name] == 'something') & (df[total amount] > 'some number')]
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
2

You function should look something like this:

def func_name(x):
    if x['Brand Name'] == 'something' and x['total amount'] > y:
        return 'some'

Use:

df.apply(func_name, axis = 1)
Mohit Motwani
  • 4,662
  • 3
  • 17
  • 45