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
in_stock which is
quantity is greater than 0 and
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.