# Module 1: Introduction to Pandas ## Learning Objectives By the end of this module, you will be able to: - Understand what Pandas is and why it is the leading data manipulation library in Python - Install Pandas and set up a reproducible Python environment - Navigate the project structure and use virtual environments - Use Git and GitHub for version control and collaboration - Write and run your first Pandas program --- ## What is Pandas? Pandas is an open-source Python library providing high-performance, easy-to-use data structures and data analysis tools. It was created by **Wes McKinney** in 2008 and has since become the backbone of the Python data science ecosystem. The name *Pandas* derives from **panel data**, a term used in econometrics for multi-dimensional structured datasets. ```python import pandas as pd print(pd.__version__) ``` ## Why Pandas? Pandas excels at: - **Tabular data handling** — similar to spreadsheets or SQL tables - **Missing data support** — built-in NaN handling - **Powerful I/O** — read/write CSV, Excel, JSON, SQL, Parquet, and more - **Flexible reshaping** — pivot, melt, stack, unstack - **Time series** — date ranges, frequency conversion, rolling windows - **Speed** — vectorised operations written in Cython/C ```{note} Pandas is built on top of NumPy. Understanding NumPy arrays is helpful but not required to start using Pandas. ``` ## Installation ### Using pip ```bash pip install pandas ``` ### Using conda ```bash conda install pandas ``` To install the full scientific Python stack: ```bash conda install pandas numpy matplotlib seaborn scikit-learn jupyter ``` ## Virtual Environments Virtual environments isolate project dependencies so different projects can use different package versions. ### Creating a virtual environment ```bash # Windows python -m venv .venv .venv\Scripts\activate # macOS / Linux python3 -m venv .venv source .venv/bin/activate ``` ### Installing project dependencies ```bash pip install pandas jupyter matplotlib pip freeze > requirements.txt ``` ```{tip} Always activate your virtual environment before installing or running any Python code. Include a `requirements.txt` file in your repository so others can reproduce your environment with `pip install -r requirements.txt`. ``` ## Project Structure A well-organised data science project follows a consistent directory layout: ``` project/ ├── data/ │ ├── raw/ │ ├── processed/ │ └── external/ ├── notebooks/ ├── src/ │ ├── data/ │ ├── features/ │ └── models/ ├── docs/ ├── tests/ ├── .gitignore ├── README.md ├── requirements.txt └── environment.yml ``` ## Git and GitHub Basics Git is a distributed version control system. GitHub is a cloud hosting service for Git repositories. ### Essential Git commands ```bash git init # Initialise a repository git add . # Stage all changes git commit -m "first commit" # Commit staged changes git remote add origin # Link to GitHub git push -u origin main # Push to GitHub ``` ### Typical workflow ```bash git checkout -b feature-branch # Create a new branch # ... make changes ... git add . git commit -m "Add new feature" git push -u origin feature-branch ``` ```{warning} Never commit data files or credentials to Git. Add sensitive files to `.gitignore`: ``` *.csv *.xls* *.pkl .env __pycache__/ .venv/ ``` ``` ## First Pandas Program ```python import pandas as pd # Create a simple DataFrame data = { "Name": ["Alice", "Bob", "Charlie"], "Age": [25, 30, 35], "City": ["New York", "Paris", "London"], } df = pd.DataFrame(data) print(df) ``` **Output:** ``` Name Age City 0 Alice 25 New York 1 Bob 30 Paris 2 Charlie 35 London ``` ## Summary - Pandas is the essential Python library for data manipulation - Virtual environments keep dependencies isolated - A consistent project structure improves reproducibility - Git and GitHub enable version control and collaboration - You can create DataFrames from Python dictionaries ## Exercises 1. Install Pandas in a fresh virtual environment and verify the installation 2. Create a new GitHub repository for this course and push your first commit 3. Write a script that creates a DataFrame with 5 rows of employee data (Name, Department, Salary, Hire Date) 4. Add a `.gitignore` file that excludes `.csv`, `.pkl`, and `.env` files ## Further Reading - [Official Pandas Documentation](https://pandas.pydata.org/docs/) - [Pandas GitHub Repository](https://github.com/pandas-dev/pandas) - [Wes McKinney's *Python for Data Analysis*](https://wesmckinney.com/book/) - [Git Pro Book](https://git-scm.com/book/en/v2)