0% found this document useful (0 votes)
4 views

Learning_NumPy_and_pandas

Uploaded by

piyushshah16op
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Learning_NumPy_and_pandas

Uploaded by

piyushshah16op
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Learning NumPy and pandas

1. NumPy (Numerical Python)

Purpose: Work with numerical data using arrays (faster and more efficient than Python lists).

Installation: Run `pip install numpy`.

Key Concepts:

1. Creating Arrays:

import numpy as np

# 1D array

arr1 = np.array([1, 2, 3])

# 2D array

arr2 = np.array([[1, 2], [3, 4]])

2. Basic Operations:

arr = np.array([1, 2, 3])

print(arr + 1) # [2, 3, 4] (element-wise addition)

print(arr * 2) # [2, 4, 6] (element-wise multiplication)

3. Useful Functions:

np.zeros((2, 3)) # 2x3 array of zeros

np.ones((3, 3)) # 3x3 array of ones

np.sum(arr) # Sum of elements

np.mean(arr) # Average

2. pandas (Panel Data)


Learning NumPy and pandas

Purpose: Work with structured data (tables).

Installation: Run `pip install pandas`.

Key Concepts:

1. Main Data Structures:

- Series: One-dimensional data (like a list with labels).

- DataFrame: Two-dimensional data (like a table).

2. Creating a DataFrame:

import pandas as pd

data = {"Name": ["Alice", "Bob"], "Age": [25, 30]}

df = pd.DataFrame(data)

print(df)

3. Reading and Writing Files:

df = pd.read_csv("file.csv") # Read a CSV file

df.to_csv("output.csv", index=False) # Save to CSV

4. Basic Operations:

print(df["Name"]) # Access a column

print(df.iloc[0]) # Access a row by position

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

5. Common Functions:
Learning NumPy and pandas

df.describe() # Summary of numerical data

df.info() # Info about columns and data types

df.sort_values("Age") # Sort by a column

df.drop("Age", axis=1) # Drop a column

You might also like