Transpose a NumPy Array to Swap Axes
📖 Scenario: Imagine you have a table of data representing sales numbers for different products across several months. The data is stored in a 2D array where rows are products and columns are months. Sometimes, you want to switch the rows and columns to analyze the data from a different perspective.
🎯 Goal: You will create a NumPy array with sales data, then use the transpose() function to swap its axes. Finally, you will print the transposed array to see the switched rows and columns.
📋 What You'll Learn
Create a 2D NumPy array named
sales with the exact values [[10, 20, 30], [40, 50, 60], [70, 80, 90]]Create a variable named
swapped_axes to hold the transposed array using sales.transpose()Print the
swapped_axes array to display the result💡 Why This Matters
🌍 Real World
Swapping axes of data arrays is common in data science when you want to change the perspective of your data, such as switching rows and columns in tables or images.
💼 Career
Understanding how to manipulate array shapes and axes is essential for data cleaning, feature engineering, and preparing data for machine learning models.
Progress0 / 4 steps