Kursinhalt
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
Operations over Series
Modifying a Single Element
In Pandas, it's easily possible to perform operations over the Series. We can modify a single element by indexing it:
import pandas as pd # Create a Series of daily temperatures (in °C) temperatures = pd.Series( [22, 18, 25, 19, 21, 23, 20], index=["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] ) print("\nUpdating Thursday's temperature to 20°C...") temperatures["Thursday"] = 20 print(temperatures)
Modifying the Entire Series
Pandas makes it very simple to perform operations over all the elements of a Series:
import pandas as pd # Create a Series of daily temperatures (in °C) temperatures = pd.Series( [22, 18, 25, 19, 21, 23, 20], index=["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] ) print("\nIncreasing all temperatures by 2°C...") temperatures = temperatures + 2 print(temperatures)
We can perform operations over slices of the Series as well. This gives us control over how much data to change:
import pandas as pd # Create a Series of daily temperatures (in °C) temperatures = pd.Series( [22, 18, 25, 19, 21, 23, 20], index=["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] ) print("\nDecreasing temperatures between Wednesday and Friday by 1°C...") temperatures['Wednesday':'Friday'] -= 1 print(temperatures)
Operations on Series
Pandas also gives us the ability to perform arithmetic operations over the corresponding elements of two or more series with ease.
The following example finds the average between the temperatures of two cities:
import pandas as pd # Temperatures in City A (in °C) city_a = pd.Series( [22, 18, 25, 19, 21, 23, 20], index=["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] ) # Temperatures in City B (in °C) city_b = pd.Series( [20, 19, 24, 20, 22, 21, 19], index=["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] ) # Add the two series to get total temperatures total_temperatures = city_a + city_b # Calculate average temperature average_temperatures = total_temperatures / 2 print("City A temperatures:") print(city_a) print("\nCity B temperatures:") print(city_b) print("\nAverage temperatures:") print(average_temperatures)
1. You have the following Series representing the number of books read each month. Which line correctly updates the number of books read in March to 6?
2. You want to increase all values in the following Series by 10
. Which line of code correctly applies this operation?
Select all that apply.
3. Which of the following lines reduces sales on Tuesday to Thursday by 10
?
Select all that apply.
4. What is the result of apples + oranges
?
War alles klar?
Danke für Ihr Feedback!
Abschnitt 1. Kapitel 10