Module 13 of 15 · Python for AI — Complete Beginner Course · Beginner

Working with NumPy and Pandas

Duration: 5 min

NumPy and Pandas are the foundation of data science in Python. NumPy handles numerical computing, Pandas handles data analysis.

Learn more: https://docs.python.org/3/tutorial/

NumPy: Numerical Computing

import numpy as np

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

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

# Array operations
print(arr * 2)      # [2 4 6 8 10]
print(arr + 10)     # [11 12 13 14 15]
print(np.sqrt(arr)) # [1. 1.41... 1.73... 2. 2.23...]

# Array functions
print(np.mean(arr))  # 3.0
print(np.sum(arr))   # 15
print(np.max(arr))   # 5
print(np.min(arr))   # 1

Try it in Google Colab: Open in Colab

[1 2 3 4 5]
[[1 2 3]
 [4 5 6]]
[ 2  4  6  8 10]
[11 12 13 14 15]
[1.         1.41421356 1.73205081 2.         2.23606798]
3.0
15
5
1

Pandas: Data Analysis

import pandas as pd

# Create a DataFrame
data = {
    'Name': ['Alice', 'Bob', 'Charlie'],
    'Age': [25, 30, 35],
    'City': ['NYC', 'LA', 'Chicago']
}
df = pd.DataFrame(data)
print(df)

# Access columns
print(df['Name'])
print(df['Age'].mean())  # 30.0

# Filter rows
print(df[df['Age'] > 25])

# Read CSV
df = pd.read_csv('data.csv')

# Write CSV
df.to_csv('output.csv', index=False)
      Name  Age     City
0    Alice   25      NYC
1      Bob   30       LA
2  Charlie   35  Chicago
0    Alice
1      Bob
2  Charlie
Name: Age, dtype: int64
30.0
     Name  Age     City
1      Bob   30       LA
2  Charlie   35  Chicago

💡 Tip: NumPy is for numerical computing, Pandas is for data analysis. Use both together!

❓ What does np.mean() do?

# Advanced example for Working with NumPy and Pandas
# Production-ready pattern
print('Advanced implementation')
Advanced implementation

❓ What is a best practice when working with Working with NumPy and Pandas?

💡 Tip: Pro Tip: Master Working with NumPy and Pandas thoroughly before moving to advanced topics. This foundation is crucial for writing professional Python code.

← Previous Continue interactively → Next →

Related Courses