Sie sind auf Seite 1von 4

Name

BELMONTE, Bianca Lou F.


APPLIED DATA SCIENCE
2nd Qtr SY 2019-2020

WORKSHEET #3: IMPORTING DATA IN PYTHON

1 Date: 12/12/19
Import and print out the first five lines of the the_zen_of_python.txt. Write the code here and submit a copy of the
output through WS3-01: P03 Importing Data in Python (#1)
Code

with open('the_zen_of_phyton.txt','r') as text1:


print(text1.readline())
print(text1.readline())
print(text1.readline())
print(text1.readline())
print(text1.readline())

2 Date: 12/12/19
Which of the following file types is not a flat file?
A. A .csv file
C
B. A tab-delimited.txt
C. A relational database

3 Date: 12/12/19
Which of the following statements about flat files is incorrect?
A. Flat files consist of rows and each row is called a record.
B. Flat files consist of multiple tables with structured relationships between the tables.
B
C. A record in a flat file is composed of fields or attributes, each of which contains at most one item of
information.
D. Flat files are pervasive in data science.

Page 1 of 4
Name
BELMONTE, Bianca Lou F.
APPLIED DATA SCIENCE
2nd Qtr SY 2019-2020

WORKSHEET #3: IMPORTING DATA IN PYTHON

4 Date: 12/12/19
Use NumPy to import seaslugs.txt. This file has a text header consisting of strings and is tab-delimited. Import the data as
floats and skip the first row upon import. Use the variable name data_float to contain the data. Print the 10th element.

Execute the following code after successful import to visualize the data. Do not forget to import matplotlib.pyplot as plt
plt.scatter(data_float[:,0], data_float[:,1]
plt.xlabel(‘time(min.)’)
plt.ylabel(‘percentage of larvae’)
plt.show()

Write the code here, as well as the print() output. Submit a copy of the plot through WS3-04: Importing Data in Python (#4).
Code

import numpy as np
data_float = np.loadtxt('seaslugs.txt', skiprows=1, dtype= float)
print(data_float[9])

import matplotlib.pyplot as plt


plt.scatter(data_float[:,0], data_float[:,1])
plt.xlabel('time (min.)')
plt.ylabel('percentage of larvae')
plt.show()

Output
[0. 0.357]

5 Date: 12/12/19
Import titanic.csv using pandas. Execute the following code after successful import to visualize the data. Do not forget to import
matplotlib.pyplot as plt.
pd.DataFrame.hist(data[['Age']])
plt.xlabel('Age (years)')
plt.ylabel('count')
plt.show()

Write the code here. Submit a copy of the plot through WS3-05: Importing Data in Python (#5).
Code

import pandas as pd
import matplotlib.pyplot as plt
titanic = pd.read_csv('titanic.csv')
pd.DataFrame.hist(titanic[['Age']])
plt.xlabel('Age (years)')
plt.ylabel('count')
plt.show()

Page 2 of 4
Name
BELMONTE, Bianca Lou F.
APPLIED DATA SCIENCE
2nd Qtr SY 2019-2020

WORKSHEET #3: IMPORTING DATA IN PYTHON

6 Date: 12/12/19
Import battledeath.xlsx and assign this to variable df. Determine the sheet names.
Code

import pandas as pd
df = pd.ExcelFile('battledeath.xlsx')
print(df.sheet_names)

Output

['2002', '2004']

7 Date: 12/12/19
Using the previous import, parse the first sheet, skip the first row and rename the columns to ‘Country’ and ‘AAM due to
War(2012)’. Assign to df1 and print its head.
Code

import pandas as pd
df = pd.ExcelFile('battledeath.xlsx')
df1= df.parse(0, skiprows=[0], names=['Country', 'AAM due to War (2002)'])
print(df1.head())

Output

Country AAM due to War (2002)


0 Albania 0.128908
1 Algeria 18.314120
2 Andorra 0.000000
3 Angola 18.964560
4 Antigua and Barbuda 0.000000

Page 3 of 4
Name
BELMONTE, Bianca Lou F.
APPLIED DATA SCIENCE
2nd Qtr SY 2019-2020

WORKSHEET #3: IMPORTING DATA IN PYTHON

8 Date: 12/12/19
Import ja_data2.mat and assign to mat. Determine the data type. Do not forget to import the necessary package for this. Write
the code here, as well as the print() output.
Code

import scipy.io as sio


mat = sio.loadmat('ja_data2.mat')
print(type(mat))

Output
<class 'dict'>

Page 4 of 4

Das könnte Ihnen auch gefallen