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

Pandas_Numpy[1]

Uploaded by

Dhruv Saraogi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views7 pages

Pandas_Numpy[1]

Uploaded by

Dhruv Saraogi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Payload: Pandas and Numpy

Easy

1. Create a NumPy Array

Write a Python program to create a NumPy array with values ranging from 1 to 10.

o Input: None

o Output:

o array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

2. Select a Column from a DataFrame

Given the DataFrame below, write a Pandas program to select the Name column.

o Input:

Python

df = pd.DataFrame({

'Name': ['Alice', 'Bob', 'Charlie'],

'Age': [25, 30, 35],

'City': ['New York', 'Los Angeles', 'Chicago']

})

o Output:

o 0 Alice

o 1 Bob

o Name: Name, dtype: object

3. Create a DataFrame from a Dictionary

Write a Python program to create a Pandas DataFrame from the dictionary below.

o Input:

Python

data = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}

o Output:
o A BC

o 0 1 4 7

o 1 2 5 8

o 2 3 6 9

Medium

4. Find the Mean of a NumPy Array

Write a Python program to find the mean of a given NumPy array.

o Input:

o array([10, 20, 30, 40, 50])

o Output:

o 30.0

5. Filter Rows by Condition in Pandas

Given the DataFrame below, write a Pandas program to filter rows where the Age is
greater than 30.

o Input:

Python

df = pd.DataFrame({

'Name': ['Alice', 'Bob', 'Charlie'],

'Age': [25, 30, 35],

'City': ['New York', 'Los Angeles', 'Chicago']

})

o Output:

o Name Age City

o 2 Charlie 35 Chicago

6. Generate a Random NumPy Array

Write a Python program to generate a 3x3 matrix of random values between 0 and 1
using NumPy.

o Input: None
o Output: (Values may vary)

o array([[0.329, 0.957, 0.604],

o [0.158, 0.940, 0.136],

o [0.410, 0.234, 0.732]])

7. Calculate the Sum of Each Column in a DataFrame

Given the DataFrame below, write a Pandas program to calculate the sum of each
column.

o Input:

Python

df = pd.DataFrame({

'A': [1, 2, 3],

'B': [4, 5, 6],

'C': [7, 8, 9]

})

o Output:

o A 6

o B 15

o C 24

o dtype: int64

8. Replace Missing Values in a DataFrame

Write a Pandas program to replace missing values (NaN) in the DataFrame with the
mean of the respective column.

o Input:

Python

df = pd.DataFrame({

'A': [1, np.nan, 3],

'B': [4, 5, np.nan],

'C': [7, 8, 9]

})
o Output:

o A B C

o 0 1.0 4.0 7

o 1 2.0 5.0 8

o 2 3.0 4.5 9

Hard

9. Find the Dot Product of Two NumPy Arrays

Write a Python program to calculate the dot product of two NumPy arrays.

o Input:

Python

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

array_2 = np.array([4, 5, 6])

o Output:

o 32

10. Group By and Aggregate in Pandas

Given the DataFrame below, write a Pandas program to group by the City column
and calculate the mean Age for each city.

• Input:

Python

df = pd.DataFrame({

'Name': ['Alice', 'Bob', 'Charlie', 'David'],

'Age': [25, 30, 35, 40],

'City': ['New York', 'Los Angeles', 'Chicago', 'New York']

})

• Output:

• Age

• City
• Chicago 35.0

• Los Angeles 30.0

• New York 32.5

11. Apply a Function to a DataFrame Column

Write a Python program to apply a custom function to the Age column of the
DataFrame below to convert the ages from years to months.

• Input:

Python

df = pd.DataFrame({

'Name': ['Alice', 'Bob', 'Charlie'],

'Age': [25, 30, 35]

})

def years_to_months(age):

return age * 12

• Output:

• Name Age

• 0 Alice 300

• 1 Bob 360

• 2 Charlie 420

12. Concatenate DataFrames

Write a Pandas program to concatenate the two DataFrames below.

• Input:

Python

df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})

df2 = pd.DataFrame({'A': [5, 6], 'B': [7, 8]})

• Output:

• AB
• 0 1 3

• 1 2 4

• 0 5 7

• 1 6 8

13. Find the Eigenvalues and Eigenvectors

Write a Python program to find the eigenvalues and eigenvectors of a 2x2 matrix
using NumPy.

• Input:

Python

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

• Output:

• Eigenvalues: [5.236, 1.764]

• Eigenvectors:

• [[ 0.894, -0.707],

• [ 0.447, 0.707]]

14. Reshape a NumPy Array

Write a Python program to reshape a 1D NumPy array into a 3x3 matrix.

• Input:

Python

array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])

• Output:

• array([[1, 2, 3],

• [4, 5, 6],

• [7, 8, 9]])

15. Pivot a DataFrame

Given the DataFrame below, write a Pandas program to pivot the DataFrame so that
the Name column becomes the index, and the Subject column values become
columns with the corresponding Score.

• Input:
Python

df = pd.DataFrame({

'Name': ['Alice', 'Bob', 'Alice', 'Bob'],

'Subject': ['Math', 'Math', 'Science', 'Science'],

'Score': [85, 78, 92, 81]

})

• Output:

• Math Science

• Name

• Alice 85 92

• Bob 78 81

This sheet covers fundamental and advanced topics in Pandas and NumPy,
enabling you to test a broad range of data manipulation and computational skills.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy