Pandas

Certainly! Pandas is a powerful library for data manipulation and analysis in Python. It provides high-level data structures and functions designed to make data analysis fast and easy. Here's a basic tutorial to get you started with Pandas:

Installation

First, ensure you have Pandas installed. You can install it using pip if it's not already installed:

pip install pandas

Importing Pandas

Once installed, you can import Pandas in your Python script or interactive session:

import pandas as pd

Pandas Data Structures

1. Series

A Pandas Series is a one-dimensional labeled array capable of holding data of any type (integer, float, string, etc.). It is similar to a one-dimensional NumPy array but with additional capabilities.

  • Creating a Series:
# From a Python list
data = [1, 2, 3, 4, 5]
s = pd.Series(data)
print(s)

# With custom index
s = pd.Series(data, index=['a', 'b', 'c', 'd', 'e'])
print(s)

2. DataFrame

A DataFrame is a two-dimensional labeled data structure with columns of potentially different types. It is like a spreadsheet or SQL table, or a dictionary of Series objects.

  • Creating a DataFrame:
# From a dictionary of lists or arrays
data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'],
        'Age': [25, 30, 35, 40],
        'City': ['New York', 'Los Angeles', 'Chicago', 'Houston']}
df = pd.DataFrame(data)
print(df)

# Specifying index
df = pd.DataFrame(data, index=['id1', 'id2', 'id3', 'id4'])
print(df)

Reading and Writing Data

1. Reading Data

Pandas can read data from various formats such as CSV, Excel, SQL databases, and more.

  • From CSV:
# Reading from CSV file
df = pd.read_csv('file.csv')
  • From Excel:
# Reading from Excel file
df = pd.read_excel('file.xlsx', sheet_name='Sheet1')

2. Writing Data

You can also write Pandas DataFrames back to CSV, Excel, or other formats.

  • To CSV:
# Writing to CSV file
df.to_csv('new_file.csv', index=False)

Basic Operations with DataFrames

1. Viewing Data

# Display first few rows
print(df.head())

# Display last few rows
print(df.tail())

# Display summary information
print(df.info())

# Display descriptive statistics
print(df.describe())

2. Selection and Indexing

# Selecting columns
print(df['Name'])
print(df[['Name', 'Age']])

# Selecting rows by index label
print(df.loc['id1'])

# Selecting rows by integer index
print(df.iloc[0])

# Selecting rows and columns
print(df.loc['id1', 'Age'])
print(df.iloc[0, 1])

3. Filtering Data

# Filtering based on a condition
print(df[df['Age'] > 30])

# Using multiple conditions
print(df[(df['Age'] > 30) & (df['City'] == 'Chicago')])

4. Data Manipulation

# Adding a new column
df['Salary'] = [50000, 60000, 70000, 80000]
print(df)

# Dropping columns
df.drop(['City'], axis=1, inplace=True)
print(df)

# Sorting DataFrame
df.sort_values(by='Age', ascending=False, inplace=True)
print(df)

Further Learning

This tutorial covers the basics of Pandas. For more advanced topics such as handling missing data, merging/joining DataFrames, groupby operations, and time series analysis, you can refer to the Pandas documentation.

Pandas is widely used in data analysis and manipulation tasks due to its versatility and powerful functionalities. Mastering Pandas will greatly enhance your ability to work with structured data effectively in Python.