Need help with pandas lambda syntax

Hello! Could you please debug the code for the seed_request variable for me? Thank you in advance!!! :sweat_smile: :sweat_smile: :sweat_smile:

Problem:

Select all rows where location is equal to Brooklyn and product_type is equal to seeds and save them to the variable seed_request.

This code is for the Pandas project:

import codecademylib
import pandas as pd

inventory = pd.read_csv('inventory.csv')
#print(inventory.head())
staten_island = inventory.iloc[:11]
print(staten_island)
product_request = staten_island.product_description
#print(product_request)

seed_request = inventory.apply(lambda row: row.product_description if row.location == 'Brooklyn' and row.product_type == 'seeds', axis=1)

Outputs:

File "script.py", line 11

seed_request = inventory.apply(lambda row: row.product_description if row.location == 'Brooklyn' and row.product_type == 'seeds', axis=1)

^

SyntaxError: invalid syntax

The dataframe itself (there are items located in Brooklyn below):

        location  product_type product_description  quantity  price
0  Staten Island         seeds               daisy         4   6.99
1  Staten Island         seeds          calla lily        46  19.99
2  Staten Island         seeds              tomato        85  13.99
3  Staten Island  garden tools                rake         4  13.99
4  Staten Island  garden tools         wheelbarrow         0  89.99

try breaking the seed_request apart, see if you can get it to just do a simple lambda action, then add the if, then the and, then the axis

Thank you for response!
I’ve tried it out and it seems like the problem starts to appear when I add ‘if’ statement. I can’t see the error.
I followed this lesson, yet I still getting the error.

I also tried to make sure that the lambda function is the culprit, and it seems that it is:

func = lambda row: row.product_description if row.location == 'Brooklyn' and row.product_type == 'seeds'
seed_request = inventory['location', 'product_type', 'product_description'].apply(func, axis=1)
print(seed_request)

Outputs:

File "script.py", line 11
    func = lambda row: row.product_description if row.location == 'Brooklyn' and row.product_type == 'seeds'
                                                                                                           ^
SyntaxError: invalid syntax
1 Like

Ok, so I’ve been playing with lambdas in the terminal because this wasn’t sitting right with me.

It’s a syntax thing, lambda: 4 if True yields an error while lambda: 4 if True else False is a perfectly kosher expression. I guess every if requires an else with lambdas.

adding an and statement should work: lambda: 4 if True and 'four' else False

1 Like

It really worked!!! Thank you very much :grin: :grin: You’ve really helped me out!

1 Like

This works too (and it avoids the unnecessary rows with False):

seed_request = inventory.product_description[(inventory.location == ‘Brooklyn’) & (inventory.product_type == ‘seeds’)]

2 Likes

suffixing the expression with “.unique()” returns a list of the items without the indices

In response to anatolyrozhkov, this kind of works:

func = lambda row: row.product_description if (row.location == 'Brooklyn' and row.product_type == 'seeds') else ""

Then invoke it with:

seed_request = inventory.apply(func, axis=1).unique()

You still get an extraneous “” item, but lambdas are apparently defined that they HAVE to return something.

You could use a list comprehension to get around this, or do thIs:

seed_request = inventory.product_description[(inventory.location == "Brooklyn") &
                                             (inventory.product_type == "seeds")].unique()