Art - Int Lab 08 Solved

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Artificial Intelligence (SWE-314) SSUET/QR/114

Lab # 8

Dataset Preparation with Excel spreadsheet

OBJECTIVE
Dataset preparation by selecting all the possible features on the given scenario
using excel. Load designated data set to working environment.

Lab Tasks:

1. Write a python code to load an excel spreadsheet containing two different sheets and print
both of them.

Output:
# import openpyxl module

Import openpyxl
Wb = openpyxl.Workbook()

# Get workbook active sheet


# from the active attribute

Sheet = wb.active

# Cell objects also have row, column


# and coordinate attributes that provide
# location information for the cell.

# Note: The first row or column integer


# is 1, not 0. Cell object is created by
# using sheet object’s cell() method.

C1 = sheet.cell(row = 1, column = 1)

# writing values to cells

C1.value = “Hello”
C2 = sheet.cell(row= 1 , column = 2)
C2.value = “World”
C3 = sheet[‘A2’]
1
2020-SE-002
Section: A
Artificial Intelligence (SWE-314) SSUET/QR/114

C3.value = “Welcome”
C4 = sheet[‘B2’]
C4.value = “Everyone”
Wb.save(“sample.xlsx”)

Output:

2. Write a python code to generate a pandas data frame having 4 columns and 5 rows. Column 1
must contain the index values like Ali, Amir, Kamran, etc and Row 1 must contain the
subject names.
Program
Import pandas as pd
Import numpy as np

Exam_data = {‘name’: [‘Ahsan’, ‘Aamir’, ‘Kamran’, ‘Saqib’, ‘Shehroz’, ‘Fahad’],


‘sujects’: [‘maths’, ‘bio’, ‘computer’, ‘chemistry’, ‘linear algebra’, ‘analogue electronics’]}
Labels = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’]
Df = pd.DataFrame(exam_data , index=labels)
Print(df)
Output:

2
2020-SE-002
Section: A
Artificial Intelligence (SWE-314) SSUET/QR/114

3. Write a python code to read an excel spreadsheet and only print first two columns using
pandas data frame.
Program:
import pandas as pd

import numpy as np

exam_data = {'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin',
'Jonas'],

'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],

'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']

df = pd.DataFrame(exam_data , index=labels)

print("Select specific columns:")

print(df[['name', 'score']])
Output:

3
2020-SE-002
Section: A
Artificial Intelligence (SWE-314) SSUET/QR/114

4. Write a python code to skip the first two rows of excel spreadsheet and print the output using
pandas data frame.
Program:
Import pandas as pd

D = {‘col1’: [1, 2, 3, 4, 7, 11], ‘col2’: [4, 5, 6, 9, 5, 0], ‘col3’: [7, 5, 8, 12, 1,11]}

Df = pd.DataFrame(data=d)

Print(“Original DataFrame”)

Print(df)

Print(“\nAfter removing first 3 rows of the said DataFrame:”)

Df1 = df.iloc[3:]

Print(df1)

Output:

4
2020-SE-002
Section: A

You might also like