0% found this document useful (0 votes)
3 views7 pages

Basic Practical Programs for NumPyand Pandas

The document provides a beginner-level guide to using NumPy and Pandas in Python, including basic operations such as creating arrays, performing mathematical operations, and handling data structures. It covers essential functions for both libraries, including array manipulation in NumPy and data management in Pandas. Additionally, it highlights the differences between NumPy and Pandas, along with practical examples and installation instructions.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

Basic Practical Programs for NumPyand Pandas

The document provides a beginner-level guide to using NumPy and Pandas in Python, including basic operations such as creating arrays, performing mathematical operations, and handling data structures. It covers essential functions for both libraries, including array manipulation in NumPy and data management in Pandas. Additionally, it highlights the differences between NumPy and Pandas, along with practical examples and installation instructions.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Basic Practical Programs for NumPy (Beginner Level)

1. Import NumPy
import numpy as np

2. Create a NumPy Array


import numpy as np

arr = [Link]([1, 2, 3, 4, 5])

print(arr)
Output
[1 2 3 4 5]

3. Create Array with Zeros


import numpy as np

arr = [Link](5)

print(arr)
Output
[0. 0. 0. 0. 0.]

4. Create Array with Ones


import numpy as np

arr = [Link](4)

print(arr)
Output
[1. 1. 1. 1.]

5. Create Array Using arange()


import numpy as np

arr = [Link](1, 10)

print(arr)
Output
[1 2 3 4 5 6 7 8 9]

6. Create Even Numbers Array


import numpy as np

arr = [Link](2, 21, 2)

print(arr)
Output
[ 2 4 6 8 10 12 14 16 18 20]

7. Find Shape of Array


import numpy as np

arr = [Link]([[1, 2], [3, 4]])

print([Link])
Output
(2, 2)

8. Array Addition
import numpy as np

a = [Link]([1, 2, 3])
b = [Link]([4, 5, 6])

print(a + b)
Output
[5 7 9]

9. Array Multiplication
import numpy as np

a = [Link]([1, 2, 3])

print(a * 2)
Output
[2 4 6]

10. Find Maximum and Minimum


import numpy as np

arr = [Link]([5, 2, 8, 1, 9])

print("Maximum:", [Link](arr))
print("Minimum:", [Link](arr))
Output
Maximum: 9
Minimum: 1

11. Find Sum and Average


import numpy as np

arr = [Link]([10, 20, 30, 40])

print("Sum:", [Link](arr))
print("Average:", [Link](arr))
Output
Sum: 100
Average: 25.0

12. Reshape Array


import numpy as np

arr = [Link]([1, 2, 3, 4, 5, 6])

new_arr = [Link](2, 3)

print(new_arr)
Output
[[1 2 3]
[4 5 6]]

13. Random Numbers Array


import numpy as np

arr = [Link](1, 100, 5)

print(arr)
Example Output
[45 12 88 34 67]

14. Indexing in NumPy Array


import numpy as np

arr = [Link]([10, 20, 30, 40])

print(arr[2])
Output
30

15. Slicing in NumPy Array


import numpy as np

arr = [Link]([1, 2, 3, 4, 5, 6])

print(arr[1:5])
Output
[2 3 4 5]

16. 2D Array Example


import numpy as np

arr = [Link]([[1, 2, 3],


[4, 5, 6]])

print(arr)
Output
[[1 2 3]
[4 5 6]]

17. Transpose of Matrix


import numpy as np

arr = [Link]([[1, 2],


[3, 4]])

print(arr.T)
Output
[[1 3]
[2 4]]

18. Identity Matrix


import numpy as np

arr = [Link](3)

print(arr)
Output
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]

19. Check Data Type


import numpy as np

arr = [Link]([1, 2, 3])

print([Link])
Output
int64

20. Convert List into NumPy Array


import numpy as np

lst = [5, 10, 15, 20]

arr = [Link](lst)

print(arr)
Output
[ 5 10 15 20]

What is Pandas in Python?


pandas is a Python library used for:
 Storing data
 Organizing data
 Cleaning data
 Analyzing data
It works like an Excel sheet inside Python.
Why Pandas is Used?
Pandas helps to work with:
 Tables
 Rows and columns
 CSV files
 Excel files
 Large datasets
It is widely used in:
 Data Science
 Data Analysis
 Machine Learning
 Business Reports

Install Pandas
pip install pandas

Import Pandas
import pandas as pd
pd is a short name for pandas.

Main Data Structures in Pandas


1. Series
A Series is a single column of data.
Example:
import pandas as pd

data = [Link]([10, 20, 30, 40])

print(data)
Output
0 10
1 20
2 30
3 40
dtype: int64

2. DataFrame
A DataFrame is a table with rows and columns.
Example:
import pandas as pd

data = {
"Name": ["Riya", "Aman", "Neha"],
"Marks": [85, 90, 78]
}

df = [Link](data)

print(df)
Output
Name Marks
0 Riya 85
1 Aman 90
2 Neha 78

Basic Pandas Operations


View First Rows
print([Link]())

Check Column Names


print([Link])

Get Information About Data


print([Link]())

Find Average
print(df["Marks"].mean())

Find Maximum Value


print(df["Marks"].max())

Select One Column


print(df["Name"])

Select Multiple Columns


print(df[["Name", "Marks"]])

Read CSV File


df = pd.read_csv("[Link]")

print(df)

Save Data to CSV


df.to_csv("[Link]")

Simple Real-Life Example


Imagine an Excel sheet:
Name Age City
Riya 20 Delhi
Aman 22 Mumbai
Pandas stores and manages this type of data easily.

Advantages of Pandas
 Fast data handling
 Easy filtering and sorting
 Works with Excel/CSV
 Useful for charts and analysis
 Saves time in data processing

Difference Between NumPy and Pandas


NumPy Pandas
Works with arrays Works with tables
Numerical operations Data analysis
Faster for math Better for structured data

Beginner Practice Task


import pandas as pd

student = {
"Name": ["A", "B", "C"],
"Marks": [80, 75, 90]
}

df = [Link](student)

print(df)

print("Average Marks:", df["Marks"].mean())

You might also like