forked from thomas-haslwanter/statsintro_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathS2_python.py
More file actions
52 lines (38 loc) · 1.36 KB
/
S2_python.py
File metadata and controls
52 lines (38 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
''' Solution to Exercise "Data Input" '''
# author: Thomas Haslwanter, date: Oct-2015
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import urllib
import io
import zipfile
def getDataDobson(url, inFile):
'''Extract data from a zipped-archive'''
# get the zip-archive
GLM_archive = urllib.request.urlopen(url).read()
# make the archive available as a byte-stream
zipdata = io.BytesIO()
zipdata.write(GLM_archive)
# extract the requested file from the archive, as a pandas XLS-file
myzipfile = zipfile.ZipFile(zipdata)
xlsfile = myzipfile.open(inFile)
# read the xls-file into Python, using Pandas, and return the extracted data
xls = pd.ExcelFile(xlsfile)
df = xls.parse('Sheet1', skiprows=2)
return df
if __name__ == '__main__':
# 1.1 Numpy --------------------
# Read in a CSV file, and show the top:
inFile1 = 'swim100m.csv'
data = pd.read_csv(inFile1)
print(data.head())
# Read in an excel file, and show the bottom
inFile2 = 'Table 2.8 Waist loss.xls'
xls = pd.ExcelFile(inFile2)
data = xls.parse('Sheet1', skiprows=2)
print(data.tail())
# Read in a zipped data-file from the WWW
url = r'http://cdn.crcpress.com/downloads/C9500/GLM_data.zip'
inFile = r'GLM_data/Table 2.8 Waist loss.xls'
df = getDataDobson(url, inFile)
print(df.tail())