Petal Power Inventory Project Question

I am working on this Petal Power Inventory Project: Petal Power

Although I have finished the project, but I have some questions about this project or generally, can anyone help me? Thanks.

My 1st Question
Project Q7: Petal Power wants to know how valuable their current inventory is.
Create a column called total_value that is equal to price multiplied by quantity.

My code is

inventory['total_value'] =  inventory.price * inventory.quantity

And the solution is:

inventory['total_value']  = inventory.apply(lambda row: inventory.price * inventory.quantity, axis = 1)

So the above 2 codes does the same thing, but I am wondering, why choose the lambda function while you can just use columns*columns?

And my 2nd question is, which is sort of related to this, we also learned that some functions uses “inplace = True” as parameters in this Pandas Practice. But any other questions in this Petal Power project, never used this parameters, and the manipulations done are all made on the original df, (all the codes that we uses does not create a new dataframe but the changes are made to the original). So my question is, how do I know if a function is actually making changes to the original df? Is there a easy way to memorize which functions need parameters of “inplace = True” ? Sometimes it gets confusing.

My 2nd Question
Project Question 6:
Add a column to inventory called in_stock which is True if quantity is greater than 0 and False if quantity equals 0.

my code is:

inventory['in_stock'] = inventory.apply(lambda row: 'True' if \
row['quantity'] > 0 else 'False',axis = 1)
print(inventory)

Although i got this right but this is something I always want to ask, in the above lambda function, Lambda row and row[‘quantity’], is the “row” in here a variable that is created just like “lambda x”, or it is actually a word that pandas can recognize as a horizontal line of the data in the DF? Similar to the word “columns”, is this word recognize by Pandas as it is a sort of reserved word? Thanks very much.

DC

1 Like

I don’t think the provided solutions are ever sold as the optimal choice, just a reasonable solution, you may very well find better routes to solve a problem and you can pat yourself on the back if you do :slightly_smiling_face:.

As for modifying in-place I’d say the majority of actions don’t modify in-place unless you actively choose to do so, there is an internal logic but it just takes practice to predict the operation. Unfortunately it’s not always 100% obvious so it’s worth just double checking the docs (or your output) to double check what you’re altering if you’re ever unsure. This Q/A on SO might be helpful since it’s a similar query- python - Pandas: Knowing when an operation affects the original dataframe - Stack Overflow

As for lambda “row” has no special properties here, it’s just a variable name. Try changing it if you like (a sensible name is a good idea in general though).