Pandas Numpy

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

1. What is Pandas?

Pandas is a Python library used for data manipulation and analysis. It provides data structures
like:

● Series: One-dimensional labeled arrays.


● DataFrame: Two-dimensional labeled data structure (like a table in Excel).

pip install pandas

Basic Pandas Operations


1.1 Creating DataFrames
import pandas as pd

# Create a DataFrame from a dictionary


data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'Score': [85, 90, 95]
}
df = pd.DataFrame(data)
print(df)

1.2 Reading Data


# Reading data from a CSV file
df = pd.read_csv('data.csv')
print(df)

1.3 Basic DataFrame Operations


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

# Get column names


print(df.columns)

# Select a column
print(df['Name'])

# Filter rows
filtered_df = df[df['Age'] > 28]
print(filtered_df)
1.4 Adding/Updating Columns
# Add a new column
df['Country'] = ['USA', 'UK', 'Canada']

# Update an existing column


df['Score'] = df['Score'] + 5
print(df)

2. What is NumPy?
NumPy (Numerical Python) is a library for numerical computations. It provides support for
arrays, matrices, and mathematical functions.

Installing NumPy
pip install numpy

Basic NumPy Operations


2.1 Creating Arrays
import numpy as np

# Create a 1D array
arr = np.array([1, 2, 3, 4, 5])
print(arr)

# Create a 2D array
matrix = np.array([[1, 2], [3, 4], [5, 6]])
print(matrix)

2.2 Array Operations


# Element-wise addition
arr = arr + 10
print(arr)

# Matrix multiplication
result = np.dot(matrix, matrix.T)
print(result)

# Statistical operations
print("Mean:", np.mean(arr))
print("Max:", np.max(arr))
print("Min:", np.min(arr))
2.3 Special Arrays
# Create an array of zeros
zeros = np.zeros((3, 3))
print(zeros)

# Create an array of ones


ones = np.ones((2, 2))
print(ones)

# Create an array with evenly spaced values


range_array = np.arange(0, 10, 2)
print(range_array)

3. What is Matplotlib?
Matplotlib is a library for creating static, animated, and interactive visualizations in Python.

Installing Matplotlib
pip install matplotlib

Basic Matplotlib Operations


3.1 Plotting a Line Graph
import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

plt.plot(x, y, label='Linear Growth')


plt.title('Line Graph Example')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.legend()
plt.show()

3.2 Plotting Bar Charts


categories = ['A', 'B', 'C']
values = [10, 15, 7]

plt.bar(categories, values, color='blue')


plt.title('Bar Chart Example')
plt.show()

3.3 Plotting Histograms


data = [22, 87, 5, 43, 56, 73, 55, 54, 11, 20, 51, 5, 79, 31, 27]

plt.hist(data, bins=5, color='green', edgecolor='black')


plt.title('Histogram Example')
plt.xlabel('Value Range')
plt.ylabel('Frequency')
plt.show()

3.4 Scatter Plot


x = [5, 7, 8, 7, 2, 17, 2, 9, 4, 11]
y = [99, 86, 87, 88, 100, 86, 103, 87, 94, 78]

plt.scatter(x, y, color='red')
plt.title('Scatter Plot Example')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()

You might also like