Lab Assignment-Pandas
1) (a) Create a Pandas Series and Perform Operations
1. Create a Pandas Series from the list: [5, 10, 15, 20, 25].
2. Assign custom indices: ['a', 'b', 'c', 'd', 'e'].
3. Extract the element at index 'c'.
4. Extract the first three elements of the Series.
5. Multiply all values in the Series by 2 and display the result.
2) Write a Pandas program to add, subtract, multiple and divide two Pandas Series.
Sample Series: [2, 4, 6, 8, 10], [1, 3, 5, 7, 9]
3) Write a Pandas program to compare the elements of the two Pandas Series.
Sample Series: [2, 4, 6, 8, 10], [1, 3, 5, 7, 10]
4) Write a Pandas program to convert a dictionary to a Pandas series.
Sample Series:
Original dictionary:
{'a': 100, 'b': 200, 'c': 300, 'd': 400, 'e': 800}
5) Write a Pandas program to convert a NumPy array to a Pandas series.
6) Write a Pandas program to change the order of index of a given series.
Sample Output:
Original Data Series:
A 1
B 2
C 3
D 4
E 5
7) Write a Pandas program to create the mean and standard deviation of the
data of a given Series.
8) Write a Pandas program to create a dataframe from a dictionary and display it.
Sample data: {'X':[78,85,96,80,86], 'Y':[84,94,89,83,86],'Z':[86,97,96,72,83]}
9) Write a Pandas program to create and display a DataFrame from a specified
dictionary data which has the index labels.
Sample Python dictionary data and list labels:
exam_data = {'name':[],
'score': [],
'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
a) Write a Pandas program to get the first 3 rows of a given DataFrame.
b) Write a Pandas program to select the 'name' and 'score' columns from the
following DataFrame
c) Write a Pandas program to select the rows where the number of attempts in
the examination is greater than 2.
d)Write a Pandas program to select the rows where the score is greater than 60.
e)Write a Pandas program to sort the DataFrame first by name in descending
order and then by score in ascending order.
f)Write a Pandas program to calculate the mean of all student’s score.
g)Write a Pandas program to retrieve the data of “Ravi”.
10) (a) Create a DataFrame and Display Data
1. Create a DataFrame using the following dictionary
data = {
"Name": ["John", "Anna", "Peter", "Linda"],
"Age": [28, 22, 35, 30],
"Salary": [60000, 50000, 75000, 62000]
Display the first two rows using head().
Display the last one row using tail().
Print the column "Age" only.
Retrieve the Salary of "Peter" using loc[].
Retrieve the Salary of the third row using iloc[].
Filter and display all rows where Age is greater than 25.
Display all employees with a Salary greater than 60,000.
Display all employees whose Age is between 25 and 35.
Add a new column "Bonus", where Bonus = Salary × 0.1.
Increase all employees' Salary by 5% and update the DataFrame.
Rename the "Age" column to "Employee Age".
11) Write a Pandas program to select rows where the value in the 'A' column is
greater than 4.
12) Write a Pandas program to select only the 'X' and 'Y' columns from the
DataFrame.
13) Write a Pandas program to use Boolean indexing to select rows where
column 'x' > 6.
14) Write a Pandas program to select the first three rows using iloc.
15) Write a Pandas program to use .loc to select rows based on a condition
16) Write a Pandas program to merge two DataFrames on a single column.
17) Write a Pandas program that performs a left join of two DataFrames.
18) Write a Pandas program to merge DataFrames and rename columns after
merge.
String Filtering Functions:-
Pandas provides several string functions to filter and manipulate text data in a DataFrame. You can
access these using the .str accessor.
1. Filtering Strings Using .str Methods
Function Description Example
Checks if the string
.str.startswith("X") df[df["Name"].str.startswith("A")]
starts with "X"
Checks if the string
.str.endswith("X") df[df["Email"].str.endswith(".com")]
ends with "X"
.str.contains("X") df[df["City"].str.contains("New")]
Checks if the string
Function Description Example
contains "X"
Filters using regex df[df["Phone"].str.match(r'^\d{3}-\d{3}-
.str.match("regex")
pattern matching \d{4}$')]
Filters based on
.str.len() df[df["Name"].str.len() > 5]
string length
Example:-
import pandas as pd
data = {
"Name": ["Alice", "Bob", "Charlie", "David", "Anna"],
"Email": ["alice@mail.com", "bob@xyz.com", "charlie@abc.net",
"david@work.org", "anna@edu.com"],
"City": ["New York", "Los Angeles", "Chicago", "New Delhi", "New Orleans"]
df = pd.DataFrame(data)
1.Starts With (startswith())
Find names that start with "A":
df[df["Name"].str.startswith("A")]
2. Ends With (endswith())
Find emails that end with ".com":
df[df["Email"].str.endswith(".com")]
3. Length-Based Filtering (len())
Find names longer than 5 characters:
df[df["Name"].str.len() > 5]
19. Given the following DataFrame:
import pandas as pd
data = {
"Name":[],
"Age": [],
"Salary": [],
"Department":[]
df = pd.DataFrame(data)
Filter all employees whose Age is greater than 30.
Select all employees who work in the IT department.
Find employees who have a Salary greater than 60,000.
Find employees who are older than 30 AND earn more than 60,000.
Find employees who are in HR OR have a Salary above 70,000.
Use .loc[] to retrieve all employees whose Age is between 25 and 35.
Use .loc[] to select employees in the IT department and display only "Name" and "Salary".
Find employees whose names start with "A".