oldoc63 / learningDS

Learning DS with Codecademy and Books
0 stars 0 forks source link

Adding a Column #379

Open oldoc63 opened 1 year ago

oldoc63 commented 1 year ago

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.

oldoc63 commented 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.

oldoc63 commented 1 year ago

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.

oldoc63 commented 1 year ago

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.

oldoc63 commented 1 year ago

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.

oldoc63 commented 1 year ago

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:

oldoc63 commented 1 year ago

Add a column to df called 'Margin', which is equal to the difference between Price and the Cost to Manufacture.