Data Analytics with Python - Lecture 2: Python Fundamentals

Python Installation

  • Installation Process:
    • Step 1: Type https://www.anaconda.com in the web browser's address bar.
    • Step 2: Click the download button.
    • Step 3: Download Python 3.7 version for Windows OS.
    • Step 4: Double click the downloaded file to run the application.
    • Step 5: Follow the on-screen instructions to complete the installation process.
  • Anaconda Navigator is a GUI that comes with the Anaconda distribution, providing a user-friendly interface to launch applications like Jupyter Notebook, Spyder, etc.

Jupyter Notebook

  • Why use Jupyter Notebook?
    • Edit code in a web browser.
    • Facilitates easy documentation.
    • Aids in easy demonstration.
    • User-friendly interface.
  • Python and Jupyter Interaction:
    • Jupyter application is a software package that contains both Python and the Jupyter application.
  • About Jupyter Notebook Interface:
    • The interface operates through a web browser, typically accessed via http://localhost:8888/notebooks/About_Jupyter_Notebook.ipynb.
    • Input Field Indicators:
      • Green color indicates "edit mode."
      • Blue color indicates "command mode."
  • Markdown Cells:
    • Contain documentation.
    • Text within Markdown cells is not executed as code.
  • Command Mode:
    • Allows editing of the notebook as a whole.
    • To exit edit mode, press the Escape key.
  • Execution Methods:
    • Ctrl + Enter: Executes the current cell, and the output field cannot be modified.
    • Shift + Enter: Executes the current cell, and the output field can be modified.
    • Run button: Use the Run button on the Jupyter interface to execute a cell.
  • Comments:
    • A comment line is written preceding with the # symbol.
  • Important Shortcut Keys:
    • A: Create a cell above the current cell.
    • B: Create a cell below the current cell.
    • D + D: Delete the current cell.
    • M: Convert the current cell to a Markdown cell.
    • Y: Convert the current cell to a code cell.

Fundamentals of Python Data Analysis with Pandas

  • Objectives:
    • Loading a simple delimited data file.
    • Counting the number of rows and columns loaded.
    • Determining the data type of each column.
    • Looking at different parts of the data by subsetting rows and columns.
  • Importing Libraries:
    • Import the Pandas library using the alias pd: import pandas as pd.
    • Import the NumPy library using the alias np: import numpy as np.
    • Import the Matplotlib library using the alias plt: import matplotlib.pyplot as plt.
  • Loading Data:
    • Use the read_csv function in Pandas to load a CSV file into a DataFrame.
    • Example: df = pandas.read_csv('F: /2019-20/NPTEL/2 Introduction to Python/data/gapminder-Five YearData.csv').
  • Displaying Data:
    • To show the entire DataFrame: df.
    • To display the first 5 rows, use the .head() method: df.head().
  • Inspecting Data:
    • Get the number of rows and columns using .shape: df.shape (Outputs a tuple, e.g., (1704, 6)).
    • Get the column names using .columns: df.columns (Outputs an Index object).
    • Get the data type of each column using .dtypes: df.dtypes.
  • Pandas Data Types vs. Python Data Types:
    • object: Typically represents strings.
    • int64: Represents integer numbers.
    • float64: Represents floating-point numbers (numbers with decimals).
    • datetime64: Represents dates and times (found in Python's datetime library).
  • Getting More Information:
    • Use .info() to get a summary of the DataFrame, including data types, non-null values, and memory usage: df.info().
  • Accessing Columns:
    • To get a specific column and save it as a Series (one-dimensional array): country_df = df['country'].
    • Display the first 5 observations: country_df.head().
    • Display the last 5 observations: country_df.tail().
  • Subsetting Data:
    • To select multiple columns, pass a list of column names: subset = df[['country', 'continent', 'year']].
    • Display the first few rows of the subset: subset.head().
    • Display the last few rows of the subset: subset.tail().