Python fundamentals for data analysis (NumPy, pandas, Jupyter): Worked Example — Foundations of Accelerated Data Science (NVIDIA-Certified Associate: Accelerated Data Science)
Python Fundamentals for Data Analysis: A Worked Example In the context of the NVIDIA-Certified Associate: Accelerated Data Science certification...
Python Fundamentals for Data Analysis: A Worked Example
In the context of the NVIDIA-Certified Associate: Accelerated Data Science certification, mastering Python fundamentals for data analysis is essential. This includes proficiency with NumPy, pandas, and Jupyter notebooks, which form the backbone of many GPU-accelerated data science workflows.
Scenario: Analyzing Sales Data to Identify Monthly Trends
Imagine you are given a dataset containing daily sales figures for a retail company over one year. Your task is to prepare the data and analyze monthly sales trends using Python tools.
Step 1: Setting Up the Environment with Jupyter Notebook
Jupyter notebooks provide an interactive environment for data analysis and visualization. Begin by launching a Jupyter notebook to write and execute Python code step-by-step.
Step 2: Importing Required Libraries
Import the essential libraries:
- numpy for numerical operations
- pandas for data manipulation
Code:
import numpy as np import pandas as pd
Step 3: Loading the Dataset
Assume the sales data is stored in a CSV file named sales_data.csv with columns Date and Sales. Load it into a pandas DataFrame:
df = pd.read_csv('sales_data.csv', parse_dates=['Date'])
Parsing dates ensures the Date column is recognized as datetime objects, facilitating time-based operations.
Step 4: Inspecting the Data
Check the first few rows to understand the structure:
df.head()
This helps verify data integrity and column names.
Step 5: Preparing the Data
Extract the month and year from the Date column to group sales monthly:
df['YearMonth'] = df['Date'].dt.to_period('M')
Step 6: Aggregating Monthly Sales Using pandas
Group the data by YearMonth and sum the sales:
monthly_sales = df.groupby('YearMonth')['Sales'].sum().reset_index()
This produces a DataFrame with total sales per month.
Step 7: Using NumPy for Additional Analysis
Convert monthly sales to a NumPy array for numerical computations, such as calculating the moving average:
sales_array = monthly_sales['Sales'].to_numpy() window_size = 3 moving_avg = np.convolve(sales_array, np.ones(window_size)/window_size, mode='valid')
This calculates a 3-month moving average to smooth out fluctuations.
Step 8: Visualizing Results (Optional in Jupyter)
While visualization is beyond pure Python fundamentals, Jupyter supports inline plotting using libraries like matplotlib. This step helps interpret the analysis.
Summary
This worked example demonstrates how to use Python fundamentals within a Jupyter notebook to load, prepare, and analyze data with pandas and NumPy. These skills are foundational for accelerated data science workflows, enabling efficient data manipulation before leveraging GPU acceleration.
Worked Example Recap
- Launched Jupyter notebook for interactive coding
- Imported numpy and pandas
- Loaded CSV sales data with date parsing
- Extracted monthly periods from dates
- Grouped and summed sales by month using pandas
- Converted sales data to NumPy array for numerical operations
- Computed a 3-month moving average with NumPy convolution
Mastering these steps builds the foundation for accelerated data science tasks on GPUs.
More in this topic
Ready to test your knowledge?
Put what you've learned into practice with a quick quiz and track your progress.
Test your knowledge →