Skip to content

Commit 5265cfc

Browse files
authored
Create README.md
1 parent edce07d commit 5265cfc

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

day_30_Pandas_DataFrames/README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Week 5 # Day 30: pandas - DataFrames & Series 🐼
2+
3+
Today, we get to the heart of the `pandas` library by exploring its two core data structures: **Series** and **DataFrame**. Understanding these foundational components is crucial for becoming proficient in data analysis and manipulation.
4+
5+
### Details Introduction
6+
7+
While we saw how to load data into a DataFrame yesterday, today we will learn how to create these objects from scratch and, more importantly, how to efficiently access and select the data they contain. Mastering these concepts will allow you to slice, dice, and transform your datasets with precision and confidence.
8+
9+
### Key Concepts
10+
11+
* **The `Series` Object:** A `Series` is a one-dimensional, labeled array. It is similar to a Python list or a `numpy` array but comes with an explicit index, which can be an integer, a string, or any other hashable type. Think of it as a single column of data from a spreadsheet, complete with row labels.
12+
13+
* **Example 1:** Creating a Series from a list of values.
14+
* **Example 2:** Creating a Series from a dictionary, where the keys automatically become the index.
15+
* **Example 3:** Accessing elements by both their position and their label.
16+
17+
* **The `DataFrame` Object:** A `DataFrame` is a two-dimensional, labeled data structure with columns that can hold different data types. It is the most common `pandas` object and is analogous to a spreadsheet or a SQL table.
18+
19+
* **Example 1:** Creating a DataFrame from a dictionary of lists (column-oriented data).
20+
* **Example 2:** Creating a DataFrame from a list of dictionaries (row-oriented data, often from JSON or APIs).
21+
* **Example 3:** Performing basic operations like checking the DataFrame's shape (`.shape`) and columns (`.columns`).
22+
23+
* **Indexing and Selection:** This is a vital skill for working with DataFrames. `pandas` provides powerful and flexible ways to select specific rows and columns.
24+
* **`[]` (Bracket Notation):** Used for selecting columns (e.g., `df['column_name']`).
25+
* **`.loc[]` (Label-based Indexing):** The primary method for selecting data by its explicit label. You can select rows, columns, or both using their names/labels.
26+
* **`.iloc[]` (Integer-based Indexing):** The method for selecting data by its integer position (index). This works just like standard Python list indexing.
27+
28+
### 📝 Practice Exercises
29+
30+
1. Create a DataFrame with columns for `Product`, `Price`, and `Quantity`. Populate it with at least 5 rows of data.
31+
2. Using the DataFrame you created, use `.loc[]` to select and print only the rows where the product is a "Laptop" or "Monitor".
32+
3. Use `.iloc[]` to select and print the `Price` and `Quantity` columns for the first two rows of your DataFrame.
33+
34+
### ✨ Best Practices & Professional Notes
35+
36+
* **Explicit is Better:** While `df['col']` is a common shortcut for selecting a column, always use `.loc[]` or `.iloc[]` when selecting rows and columns simultaneously. This prevents **chained indexing warnings** (`SettingWithCopyWarning`) and makes your code more readable.
37+
* **Vectorization:** `pandas` operations are "vectorized," meaning they are optimized to operate on entire Series or DataFrames at once, without a `for` loop. This is much faster and more efficient.
38+
* **Data Consistency:** Always ensure your data types are correct using `df.info()`. Incorrect data types can lead to unexpected errors in calculations.
39+
40+
### 🏃 How to Run This Code
41+
42+
1. First, ensure you have `pandas` installed:
43+
```bash
44+
pip install pandas
45+
```
46+
2. Save each Python file in the directory.
47+
3. Run them from your terminal to see the output:
48+
```bash
49+
python part_1_series_intro.py
50+
python part_2_dataframe_creation.py
51+
python part_3_indexing_and_selection.py
52+
```
53+
54+
---
55+
56+
[⬅️ Back to Main Repository](./README.md)

0 commit comments

Comments
 (0)