0% found this document useful (0 votes)
21 views5 pages

Lab Session 06: Perform Following Operations Using Pandas

The document outlines Lab Session 06 for a Data Science course using Python, focusing on operations with Pandas. It includes pre-lab tasks, in-lab tasks such as creating DataFrames, concatenating them, filtering rows based on conditions, and adding new columns, along with example code snippets and outputs. Additionally, it discusses practical applications of these operations in real-world scenarios.
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)
21 views5 pages

Lab Session 06: Perform Following Operations Using Pandas

The document outlines Lab Session 06 for a Data Science course using Python, focusing on operations with Pandas. It includes pre-lab tasks, in-lab tasks such as creating DataFrames, concatenating them, filtering rows based on conditions, and adding new columns, along with example code snippets and outputs. Additionally, it discusses practical applications of these operations in real-world scenarios.
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
You are on page 1/ 5

REGD. NO.

238W1A5464 DATA SCIENCE USING PYTHON LABORATORY-23AI&DS4354 ACADEMIC YEAR: 2024-2025

Lab Session 06: Perform following operations using pandas

Date of the Session: 10/02/2025 Time of the Session: 10:20AM to 1:00PM

Pre-Lab Task: Write answers before entering into lab.


1. What is a DataFrame in Pandas, and how is it different from a NumPy array?
A. The major differences between DataFrame and Array are listed below: Numpy arrays can be multi-
dimensional whereas DataFrame can only be two-dimensional. Arrays contain similar types of objects
or elements whereas DataFrame can have objects or multiple or similar data types. Both array and
DataFrames are mutable.

2. How can we create a DataFrame in Pandas using a dictionary?


A. You can create a DataFrame in Pandas using a dictionary by passing the dictionary to the
pd.DataFrame() constructor. The keys of the dictionary will become the column names, and the
corresponding values will be the data for those columns.

3. What is the purpose of the concat() function in Pandas?


A. The concat() function in Pandas is used to combine or concatenate two or more DataFrames along a
particular axis (either rows or columns). It can be particularly useful when you need to merge datasets
that share similar columns or rows.
the key purposes:
1.Concatenating along rows (axis=0)
2. Concatenating along columns (axis=1)
3. Handling mismatched indexes

4. How can we filter rows in a DataFrame based on a condition?


A. To filter rows in a Pandas DataFrame based on a condition, you can use boolean indexing. This involves
applying a condition to the DataFrame, which results in a boolean series (True or False). The DataFrame
is then indexed using this boolean series to return the rows where the condition is True.

5. Why might we need to add a new column to a DataFrame, and how can we do it in Pandas?
A. We might need to add a new column to a DataFrame for several reasons, including:
1. Data Transformation: Creating new features from existing ones, such as combining two columns or
applying a mathematical operation.
2. Data Enrichment: Adding external information to the DataFrame, like adding a calculated field or
merging data from another source.

Ex: import pandas as pd


data = {'Name': ['Alice', 'Bob'], 'Age': [25, 30]}
df = pd.DataFrame(data)
df['Country'] = 'USA'
print(df)
df['Age_in_10_years'] = df['Age'] + 10 print(df)

LAB No. 06 VELAGAPUDI RAMAKRISHNA SIDDHARTHA ENGINEERING COLLEGE Page |


REGD. NO. 238W1A5464 DATA SCIENCE USING PYTHON LABORATORY-23AI&DS4354 ACADEMIC YEAR: 2024-2025

Output: Name Age Country Age_in_10_years


0 Alice 25 USA 35
1 Bob 30 USA 40

In Lab Task:

a. Creating dataframe
Code:
import pandas as pd
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'David'],
'Age': [25, 30, 35, 40],
'City': ['New York', 'Los Angeles', 'Chicago', 'Houston']
}
df = pd.DataFrame(data)
print("DataFrame:")
print(df)

Output:
Name Age City
0 Alice 25 New York
1 Bob 30 Los Angeles
2 Charlie 35 Chicago
3 David 40 Houston

b. concat()

Code:
data2 = {
'Name': ['Eve', 'Frank'],
'Age': [45, 50],
'City': ['Seattle', 'Boston']
}
df2 = pd.DataFrame(data2)
concat_df = pd.concat([df, df2], axis=0)
print("Concatenated DataFrame:")
print(concat_df)

Output:
Name Age City
0 Alice 25 New York
1 Bob 30 Los Angeles
2 Charlie 35 Chicago
3 David 40 Houston
0 Eve 45 Seattle
1 Frank 50 Boston

LAB No. 06 VELAGAPUDI RAMAKRISHNA SIDDHARTHA ENGINEERING COLLEGE Page |


REGD. NO. 238W1A5464 DATA SCIENCE USING PYTHON LABORATORY-23AI&DS4354 ACADEMIC YEAR: 2024-2025

c. Setting conditions

Code:
filtered_df = df[df['Age'] > 30]
print("Filtered DataFrame (Age > 30):")
print(filtered_df)

Output:
Nam Age City
2 Charlie 35 Chicago
3 David 40 Houston

d. Adding a new column

Code:
df['Age_in_5_years'] = df['Age'] + 5
print("DataFrame with New Column:")
print(df)

Output:
Name Age City Age_in_5_years
0 Alice 25 New York 30
1 Bob 30 Los Angeles 35
2 Charlie 35 Chicago 40
3 David 40 Houston 45

Post Lab Task:


A. Write a Python code snippet to create a Pandas DataFrame with at least three columns and five rows.
Code:
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'],
'Age': [24, 30, 35, 40, 22],
'City': ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Seattle']}
df = pd.DataFrame(data)
print(df)
Output:
Name Age City
0 Alice 24 New York
1 Bob 30 Los Angeles
2 Charlie 35 Chicago
3 David 40 Houston
4 Eve 22 Seattle

LAB No. 06 VELAGAPUDI RAMAKRISHNA SIDDHARTHA ENGINEERING COLLEGE Page |


REGD. NO. 238W1A5464 DATA SCIENCE USING PYTHON LABORATORY-23AI&DS4354 ACADEMIC YEAR: 2024-2025

B. Given two DataFrames, df1 and df2, how would you concatenate them vertically and horizontally?
Vertically:
df1 = pd.DataFrame({'Name': ['Alice', 'Bob'], 'Age': [24, 30]})
df2 = pd.DataFrame({'Name': ['Charlie', 'David'], 'Age': [35, 40]})
df_vertical = pd.concat([df1, df2], axis=0)
print(df_vertical)
Output:
Name Age
0 Alice 24
1 Bob 30
0 Charlie 35
1 David 40

Horizontally:
df1 = pd.DataFrame({'Name': ['Alice', 'Bob'], 'Age': [24, 30]})
df2 = pd.DataFrame({'City': ['New York', 'Los Angeles']})
df_horizontal = pd.concat([df1, df2], axis=1)
print(df_horizontal)
Output:
Name Age City
0 Alice 24 New York
1 Bob 30 Los Angeles

C. How would you filter out rows where the values in the “Age” column are greater than 25?
Code:
df = pd.DataFrame({'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'], 'Age': [24, 30, 35, 40, 22]})
filtered_df = df[df['Age'] <= 25]
print(filtered_df)
Output:
Name Age City
0 Alice 24 New York
4 Eve 22 Seattle

LAB No. 06 VELAGAPUDI RAMAKRISHNA SIDDHARTHA ENGINEERING COLLEGE Page |


REGD. NO. 238W1A5464 DATA SCIENCE USING PYTHON LABORATORY-23AI&DS4354 ACADEMIC YEAR: 2024-2025

D. If you have a DataFrame containing employee names and salaries, how would you add a new column
for a "Bonus" (10% of salary)?
Code:
df = pd.DataFrame({'Employee': ['Alice', 'Bob', 'Charlie', 'David'],
'Salary': [50000, 60000, 70000, 80000]})
df['Bonus'] = df['Salary'] * 0.10
print(df)
Output:
Employee Salary Bonus
0 Alice 50000 5000.0
1 Bob 60000 6000.0
2 Charlie 70000 7000.0
3 David 80000 8000.0

E. Explain a real-world scenario where using Pandas operations like concatenation and filtering conditions
would be beneficial.
A. In a business scenario:
 Concatenation would be used to combine sales data from multiple regions (e.g., North America and
Europe).
 Filtering would help you analyze high-performing products or employees. For example, filtering out
employees earning above a certain salary to calculate bonuses or analyzing products with revenue
greater than a specific threshold.

Students Signature

(For Evaluator’s use only)


Comment of the Evaluator (if Any) Evaluator’s Observation

Marks Secured:_______ out of ________

Signature of the Evaluator with Date:

LAB No. 06 VELAGAPUDI RAMAKRISHNA SIDDHARTHA ENGINEERING COLLEGE Page |

You might also like