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

Lab Manual 5

The document outlines the learning outcomes for a course on Artificial Intelligence, focusing on the use of Pandas and NumPy for handling Excel files. It provides detailed instructions on reading and writing Excel files using Pandas, as well as how to perform numerical operations with NumPy. Additionally, it includes exercise questions related to managing student records and generating timetables.

Uploaded by

saeeedk484
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)
7 views5 pages

Lab Manual 5

The document outlines the learning outcomes for a course on Artificial Intelligence, focusing on the use of Pandas and NumPy for handling Excel files. It provides detailed instructions on reading and writing Excel files using Pandas, as well as how to perform numerical operations with NumPy. Additionally, it includes exercise questions related to managing student records and generating timetables.

Uploaded by

saeeedk484
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/ 5

Artificial Intelligence

Department of Computer Science

University of Engineering and Technology, Lahore

Class Learning Outcomes

Students will learn about

• Pandas
• NumPy
• CSV file read and writing

Pandas
Reading and writing Excel files in Python is a common task that can be efficiently handled with the
pandas library, which provides high-level data manipulation tools and integrates well with Excel
files. Below are some examples of how you can perform these tasks.

1. Installing Required Libraries


First, make sure you have the required libraries installed. You can install them using pip:

pip install pandas openpyxl xlrd


• pandas: Provides data structures and data analysis tools.

• openpyxl: Allows you to read/write Excel 2010 xlsx/xlsm files.

• xlrd: Allows you to read older Excel files (xls).

2. Reading Excel Files


You can use pandas to read Excel files into a DataFrame. Here’s how you can read an Excel file:

import pandas as pd
# Read an Excel file
df = pd.read_excel('path_to_your_file.xlsx', sheet_name='Sheet1')
# Display the first few rows of the DataFrame
print(df.head())

• path_to_your_file.xlsx: Path to your Excel file.


• sheet_name: Name of the sheet you want to read. If you want to read the first sheet, you
can omit this argument or set it to 0.
3. Writing to Excel Files
You can also write DataFrames to Excel files using pandas:

import pandas as pd
# Create a DataFrame
data = {
'Column1': [1, 2, 3],
'Column2': ['A', 'B', 'C']
}
df = pd.DataFrame(data)

# Write the DataFrame to an Excel file


df.to_excel('path_to_your_file.xlsx', sheet_name='Sheet1', index=False)

• path_to_your_file.xlsx: Path where you want to save the Excel file.


• sheet_name: Name of the sheet to write to.
• index: Whether to write row names (index). Set to False to omit the index.

4. Handling Multiple Sheets

If you need to read or write multiple sheets, you can do so as follows:

• Reading Multiple Sheets

# Read multiple sheets into a dictionary of DataFrames


sheets = pd.read_excel('path_to_your_file.xlsx', sheet_name=None)

# Access sheets by name


df1 = sheets['Sheet1']
df2 = sheets['Sheet2']

• Writing Multiple Sheets

# Create a Pandas Excel writer object


with pd.ExcelWriter('path_to_your_file.xlsx') as writer:
df1.to_excel(writer, sheet_name='Sheet1', index=False)
df2.to_excel(writer, sheet_name='Sheet2', index=False)

5. Advanced Options

• Specify columns to read:

df = pd.read_excel('path_to_your_file.xlsx', usecols=['Column1', 'Column2'])


• Skip rows or headers:

df = pd.read_excel('path_to_your_file.xlsx', skiprows=2)

• Write with formatting: For more advanced formatting, you might need to use
openpyxl directly or use other libraries like xlsxwriter.

6. Example of Using openpyxl Directly

For more advanced use cases, like setting cell styles or adding formulas, you can use openpyxl:

from openpyxl import Workbook

# Create a new Workbook


wb = Workbook()
ws = wb.active
ws.title = 'Sheet1'

# Add data
ws['A1'] = 'Hello'
ws['B1'] = 'World'

# Save the Workbook


wb.save('path_to_your_file.xlsx')
NUMPY

While NumPy does not directly provide functionality for reading from or writing to Excel files, it
can be used in conjunction with other libraries that handle Excel files. Typically, NumPy is used
for numerical operations and data manipulation, while pandas are more commonly used for reading
and writing Excel files. However, you can combine NumPy and pandas to handle Excel data
effectively.

Here’s how NumPy can be used alongside pandas for operations involving Excel files:

Reading Excel Files with Pandas and Converting to Numpy Array

You can read an Excel file in Pandas. Data Frame and then convert that Data Frame to a NumPy
array for numerical operations.

import pandas as pd

# Read an Excel file into a DataFrame


df = pd.read_excel('path_to_your_file.xlsx', sheet_name='Sheet1')

# Convert the DataFrame to a NumPy array


data_array = df.to_numpy()

# Perform operations using NumPy


import numpy as np
mean_values = np.mean(data_array, axis=0) # Example: calculate mean of each column
print(mean_values)

Writing NumPy Arrays to Excel Files

If you have numerical data in a NumPy array and you want to write it to an Excel file, you would
typically convert it to a pandas Data Frame first.

import pandas as pd
import numpy as np

# Create a NumPy array


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

# Convert the NumPy array to a DataFrame


df = pd.DataFrame(data_array, columns=['Column1', 'Column2', 'Column3'])

# Write the DataFrame to an Excel file


df.to_excel('path_to_your_file.xlsx', sheet_name='Sheet1', index=False)

Using NumPy for Numerical Operations

NumPy is excellent for performing numerical operations on data, and it can be very efficient for
manipulating data arrays. For instance, if you read data from an Excel file into a Data Frame, you
might want to perform statistical calculations or other numerical operations using NumPy.

import pandas as pd
import numpy as np

# Read data from an Excel file


df = pd.read_excel('path_to_your_file.xlsx', sheet_name='Sheet1')

# Convert to a NumPy array


data_array = df.to_numpy()
# Perform operations, e.g., calculate standard deviation
std_devs = np.std(data_array, axis=0)
print(std_devs)

Summary

• pandas is generally used to read from and write to Excel files.


• numpy is used for numerical operations and can be used in combination with pandas to
process data read from Excel files.
• You convert data between pandas DataFrames and numpy arrays as needed to leverage the
strengths of each library.

If you need advanced Excel-specific operations (like cell formatting or formulas), openpyxl or
xlsxwriter might be more appropriate.

Exercise questions

1. Write a Python code to enter data into a CSV file for the university management tasks. The
code will demonstrate how to create the files for student records and student grades.
2. Read data from multiple sheets in an Excel file and display the first few rows of each.
3. The university needs to generate a weekly timetable for each course, ensuring no time
overlaps. Create a data frame and display the timetable using the ‘print’ function.
4. A university admin wants to identify students scoring above 85 in all subjects from the
student records. Read data from ‘student_records.xlsx’.
5. The university requires calculating students' GPA based on their scores across multiple
subjects, each having different credits. Read data from ‘student_grades.xlsx’.

You might also like