-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathAdelaide_wine_data.py
More file actions
53 lines (40 loc) · 1.25 KB
/
Adelaide_wine_data.py
File metadata and controls
53 lines (40 loc) · 1.25 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
52
"""
Adelaide Wine data
https://www.adelaide.edu.au/wine-econ/databases/
Written by Dave Backus and Alex Zhong, April 2016
Created with Python 3.5
"""
import sys
import pandas as pd
#import matplotlib.pyplot as plt
print('\nPython version: ', sys.version)
print('Pandas version: ', pd.__version__, '\n')
url = 'https://www.adelaide.edu.au/wine-econ/papers/Section_I.xlsx'
raw = pd.read_excel(url,
sheetname='Table 1',
skiprows=1,
header=[0,1,2], # sets column labels
parse_cols=[1]+list(range(3,14)),
na_values=['na']
)
print('Dimensions of raw data:', raw.shape)
print('Variable dtypes:\n', raw.dtypes, sep='')
#%%
# splice variable names together (list of 3-tuples -> list)
df = raw.copy()
names = list(df)
df.columns = [name[0]+name[1]+name[2] for name in names]
# name index
df.index.name = 'Country'
# drop rows with NaN index
df = df.reset_index('Country')
df = df[df['Country'].notnull()]
print('Dimensions after dropping bad rows:', df.shape)
# optional: drop subtotal columns
#df = df[~df['Country'].str.startswith('Total')]
# set the index as Country
df = df.set_index('Country')
df = df.astype(float)
df.dtypes
#%%
df[[1]]