Open oldoc63 opened 1 year ago
One way that we can add a new column is by giving a list of the same length as the existing DataFrame.
The DataFrame df contains information on products sold at a hardware store. Add a column to df called 'Sold in Bulk?', which indicates if the product is sold in bulk or individually.
We can also add a new column that is the same for all rows in the DataFrame. Suppose we know that all our products are currently in stock.
Add a column to df called Is Taxed?, which indicates whether or not to collect sales tax on the product. It should be 'Yes' for all rows.
Finally, you can add a new column by performing a function on the existing columns.
May be we want to add a column to our inventory table with the amount of sales tax that we need to charge for each item. The following code multiplies each Price by 0.075, the sales tax for our state:
Add a column to df called 'Margin', which is equal to the difference between Price and the Cost to Manufacture.
Sometimes, we want to add a column to an existing DataFrame. We might want to add new information or perform a calculation based on the data that we already have.