|
1 | 1 | # DataFrame Manipulation & Sorting
|
2 | 2 |
|
3 |
| -## Introduction |
| 3 | +:::{objectives} |
4 | 4 |
|
5 |
| -**DataFrame Manipulation & Sorting:** |
| 5 | +* Demonstrate effective techniques for selecting specific rows and from DataFrames. |
| 6 | +* Equip participants with skills to add and remove columns within a DataFrame. |
| 7 | +* Implement sorting methods by values, by index, and perform multiple column sorting with custom orders. |
| 8 | +::: |
6 | 9 |
|
7 |
| -* Now that we can import data, we need to reshape it for analysis |
8 |
| -* Most real-world datasets need significant manipulation before analysis |
9 |
| -* Selecting, adding, removing, and reordering data are fundamental skills |
10 |
| -* These operations build directly on our understanding of DataFrames as labeled, 2D structures |
| 10 | +:::{exercise} Time |
| 11 | +20 minutes |
| 12 | +::: |
11 | 13 |
|
12 | 14 | :::{discussion}
|
13 | 15 |
|
| 16 | +* Now that we can import data, we need to reshape and manipulate it for analysis |
14 | 17 | * In real-world data analysis, you'll spend about 80% of your time cleaning and manipulating data, and only 20% on actual analysis
|
15 |
| -* The skills we're covering today form the backbone of data wrangling in Python |
| 18 | +* The skills we're covering in this session form the backbone of data wrangling in Python |
16 | 19 | * Think of these operations as transforming raw data into analysis-ready information
|
17 | 20 |
|
18 | 21 | :::
|
@@ -296,7 +299,7 @@ Understand the different ways to select data from a DataFrame, including column
|
296 | 299 | * multiple columns
|
297 | 300 | * columns in-place
|
298 | 301 |
|
299 |
| -:::{done} |
| 302 | +:::{demo} |
300 | 303 |
|
301 | 304 | ```python
|
302 | 305 |
|
@@ -787,45 +790,14 @@ emp004 David 42 Seattle 92000 Tech Tech
|
787 | 790 |
|
788 | 791 | :::{discussion}
|
789 | 792 |
|
790 |
| -* "Sorting is essential for both data analysis and presentation." |
791 |
| -* "Multi-column sorting is particularly useful for hierarchical data." |
792 |
| -* "The `ascending` parameter can be a single boolean or a list of booleans for multi-column sorts." |
793 |
| -* "Custom sorting allows for domain-specific ordering beyond simple alphabetical or numerical order." |
794 |
| -* "The `inplace=True` parameter can be used to modify the original DataFrame rather than creating a new one." |
| 793 | +* Sorting is essential for both data analysis and presentation. |
| 794 | +* Multi-column sorting is particularly useful for hierarchical data. |
| 795 | +* The `ascending` parameter can be a single boolean or a list of booleans for multi-column sorts. |
| 796 | +* Custom sorting allows for domain-specific ordering beyond simple alphabetical or numerical order. |
| 797 | +* The `inplace=True` parameter can be used to modify the original DataFrame rather than creating a new one. |
795 | 798 |
|
796 | 799 | :::
|
797 | 800 |
|
798 |
| -**Exercise 3 - Sorting Practice:** |
799 |
| - |
800 |
| -Have students execute: |
801 |
| -```python |
802 |
| -# Continue with the final inventory DataFrame from Exercise 2 |
803 |
| -print("Original inventory:") |
804 |
| -print(inventory_final) |
805 |
| - |
806 |
| -# 1. Sort the inventory by Category, then by Price (descending) within each category |
807 |
| -sorted_by_cat_price = inventory_final.sort_values(['Category', 'Price'], |
808 |
| - ascending=[True, False]) |
809 |
| -print("\n1. Sorted by Category, then by Price (descending):") |
810 |
| -print(sorted_by_cat_price) |
811 |
| - |
812 |
| -# 2. Sort the inventory by Value (highest first) |
813 |
| -sorted_by_value = inventory_final.sort_values('Value', ascending=False) |
814 |
| -print("\n2. Sorted by Total Value (descending):") |
815 |
| -print(sorted_by_value) |
816 |
| - |
817 |
| -# 3. Advanced: Create a custom sorting order for Status |
818 |
| -# Make 'Available' come before 'Out of Stock' |
819 |
| -inventory_final['Status_Coded'] = pd.Categorical( |
820 |
| - inventory_final['Status'], |
821 |
| - categories=['Available', 'Out of Stock'], |
822 |
| - ordered=True |
823 |
| -) |
824 |
| -sorted_by_status = inventory_final.sort_values('Status_Coded') |
825 |
| -print("\n3. Sorted by Status (custom order):") |
826 |
| -print(sorted_by_status[['Product_Name', 'Status', 'Status_Coded']]) |
827 |
| -``` |
828 |
| - |
829 | 801 | :::{exercise}
|
830 | 802 |
|
831 | 803 | **Multiple Column Sorting with Custom Orders:**
|
|
0 commit comments