Course Content
Pandas: First Steps
Pandas: First Steps
1. Getting Started
Introduction to PandasSeriesTask: Coffee Orders SeriesTask: Barista's Favorites CoffeesAccessing Elements from a SeriesTask: Drink of the HourTask - Signature DrinksTask: Mid-Morning SpecialsTask - Fetching the Mid-Shift DrinksOperations over SeriesTask: Modifying a single elementTask: Modifying the Entire SeriesTask: Modifying Part of a SeriesTask: Processing the Restock of Inventory
2. Basics of Dataframes
What is a DataFrame?Task: Creating a Table of Popular Coffee DrinksTask: Tracking Coffee Bean DeliveriesTask: Weekly Coffee Sales ReportAdding new ColumnsTask: Finding Total Sales Per DrinkTask: Calculating Total Sales RevenueAdding new RowsTask: Adding a New Employee to the CafΓ© TeamTask: Adding New Coffee Types to the CafΓ© Menu
Adding new Columns
We can easily add new columns to an existing DataFrame using the indexing syntax:
js
It is important that list_of_values
has the same number of elements as there are rows in the DataFrame, otherwise you'll get an error.
Example 1: Adding a new column with a List of Values
import pandas as pd df = pd.DataFrame({ 'Member': ['Sophie', 'Liam', 'Emma'], 'Role': ['Designer', 'Developer', 'Tester'] }) # Add a new column for Project Name df['Project'] = ['Aurora', 'Aurora', 'Aurora'] print(df)
Example 2: Using values from another column
import pandas as pd df = pd.DataFrame({ 'Product': ['Laptop', 'Tablet', 'Phone'], 'Price': [1000, 600, 400] }) # Derive a new column 'Discounted Price' (10% off) df['Discounted Price'] = df['Price'] * 0.9 print(df)
1. What happens if you try to add a new column to a DataFrame with a list that has fewer elements than the number of rows?
2. Which of the following lines correctly adds a column Discount
which is 20% of the Price
?
3. What would be the output of the following code?
Everything was clear?
Thanks for your feedback!
SectionΒ 2. ChapterΒ 5