Lab Manual 5
Lab Manual 5
• 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.
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())
import pandas as pd
# Create a DataFrame
data = {
'Column1': [1, 2, 3],
'Column2': ['A', 'B', 'C']
}
df = pd.DataFrame(data)
5. Advanced Options
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.
For more advanced use cases, like setting cell styles or adding formulas, you can use openpyxl:
# Add data
ws['A1'] = 'Hello'
ws['B1'] = 'World'
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:
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
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
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
Summary
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’.