And Lists: Jason Myers
And Lists: Jason Myers
And Lists: Jason Myers
and lists
DATA T YP E S F O R DATA S C I E N C E
I NP Y T H O N
Jason Myers
Instructor
Data
types
Data type system sets the stage for the capabilities of the
language
Iterable
Mutable
Index
cookies.append('Tirggel')
sugar', 'Tirggel']
print(cookies)
print(cookies[2])
cakes = ['strawberry',
cakes print(desserts)
['chocolate chip', 'peanut butter', 'sugar',
'Tirggel', 'strawberry', 'vanilla']
position = cookies.index('sugar')
print(position)
cookies[3]
'sugar'
name = cookies.pop(position)
print(name)
sugar
print(cookies)
chocolate chip
peanut butter
Tirggel
print(cookies)
sorted_cookies = sorted(cookies)
print(sorted_cookies)
Jason Myers
Instructor
Tuple,
Tuple
Hold data in order
Index
Immutable
Pairing
Unpackable
us_num_1, in_num_1 =
top_pairs[0] print(us_num_1)
Chocolate Chip
print(in_num_1)
Punjabi
Punjabi
Chocolate Chip
Fruit Cake Rusk
Brownies
# ..etc..
('vanilla', 'chocolate')
item2 = 'butter',
print(item2)
('butter',)
Mutable
print(types_of_cookies_eaten)
types_of_cookies_eaten.add('biscotti')
types_of_cookies_eaten.add('chocolate chip')
print(types_of_cookies_eaten)
types_of_cookies_eaten.update(cookies_hugo_ate
) print(types_of_cookies_eaten)
types_of_cookies_eaten.discard('biscotti')
print(types_of_cookies_eaten)
types_of_cookies_eaten.pop()
types_of_cookies_eaten.pop()
'chocolate chip'
'anzac'
'anzac'])
cookies_jason_ate.union(cookies_hugo_ate)
set(['chocolate chip', 'anzac', 'oatmeal cream', 'peanut butter'])
cookies_jason_ate.intersection(cookies_hugo_ate)
set(['chocolate chip'])
cookies_jason_ate.difference(cookies_hugo_ate)
cookies_hugo_ate.difference(cookies_jason_ate)
set(['anzac'])
Jason Myers
Instructor
Creating and looping through
dictionaries
Hold data in key/value pairs
Iterable
Created by dict() or {}
art_galleries = {}
|
KeyError Traceback (most recent call last)
<ipython-input-1-4f51c265f287> in <module>()
--> 1 art_galleries['Louvre']
KeyError: 'Louvre'
If you ask for a key that does not exist that will stop your
program from running in a KeyError
'Not Found'
'10011'
print(art_galleries['10027'])
'(212) 368-4941'
Jason Myers
Instructor
Adding and extending
dictionaries
Assignment to add a new key/value to a dictionary
print(galleries_10007)
art_galleries['10007'] = galleries_10007
del art_galleries['11234']
galleries_10310 =
art_galleries.pop('10310')
print(galleries_10310)
{'New Dorp Village Antiques Ltd': '(718) 815-2526'}
Jason Myers
Instructor
Working with dictionaries more
py.items()
thonicall
methody
returns an object we can iterate over
'11234' in art_galleries
False
if '10010' in art_galleries:
Jason Myers
Instructor
CSV
Files
NAME,TEL,ADDRESS1,ADDRESS2,CITY,ZIP
O'reilly William & Co Ltd,(212) 396-1822,52 E 76th St,,New York,10021
import csv
csvfile = open('ART_GALLERY.csv',
'r') for row in csv.reader(csvfile):
print(row)
csvfile.close()
Jason Myers
Instructor
Collections
Module
Part of Standard Library Advanced
data containers
Counter({'Mobile Food Truck': 114, 'Food Cart': 74, 'Snack Bar': 24,
'Specialty Cart': 18, 'Restaurant': 15, 'Fruit & Vegetable Cart': 4})
print(nyc_eatery_count_by_types['Restaurant'])
15
print(nyc_eatery_count_by_types.most_common(3)
)
eateries_by_park[park_id].append(name)
print(eateries_by_park['M010'])
{'MOHAMMAD MATIN','PRODUCTS CORP.', 'Loeb Boathouse Restaurant',
'Nandita Inc.', 'SALIM AHAMED', 'THE NY PICNIC COMPANY',
'THE NEW YORK PICNIC COMPANY, INC.', 'NANDITA, INC.', ...}
print(nyc_eatery_permits.popitem())
print(nyc_eatery_permits.popitem())
print(nyc_eatery_permits.popitem(last=False))
Jason Myers
Instructor
What is a namedtuple?
A tuple where each position (column) has a name
Ensure each one has the same properties Alternative to
a pandas DataFrame row
06/11/2016
2016-06-11 00:00:00
date_dt.strftime('%m/%d/%Y')
'06/11/2016'
date_dt.isoformat()
'2016-06-11T00:00:00'
daily_violations = defaultdict(int)
for violation in parking_violations:
violation_date = datetime.strptime(violation[4],
'%m/%d/%Y')
daily_violations[violation_date.day] += 1
2017-05-05 12:30:00.740415
2017-05-05 17:30:05.467221
2016-07-12 04:39:00-04:00
print(la_dt)
2016-07-12 01:39:00-07:00
2016-07-12 04:39:00
2016-04-13 04:39:00
print(record_dt + flashback)
2016-10-10 04:39:00
datetime.timedelta
print(time_diff)
0:00:04
Jason Myers
Instructor
Parsing time with
pend uluwillmatempt to convert a string to a pendulum
.parse()
datetime object without the need of the format string
import pendulum
print(occured_dt)
'2016-06-11T14:38:00-04:00'
print(violation_dts)
[<Pendulum [2016-06-11T14:38:00-04:00]>,
<Pendulum [2016-04-25T14:09:00-04:00]>,
<Pendulum [2016-04-23T07:49:00-04:00]>,
<Pendulum [2016-04-26T07:09:00-04:00]>,
<Pendulum [2016-01-04T09:52:00-05:00]>]
2016-06-12T03:38:00+09:00
2016-04-26T03:09:00+09:00
2016-04-23T20:49:00+09:00
2016-04-26T20:09:00+09:00
2016-01-04T23:52:00+09:00
print(pendulum.now('Asia/Tokyo'))
<Pendulum [2017-05-06T08:20:40.104160+09:00]>
print(diff.in_words())
print(diff.in_hours())
71
csvfile = open('ART_GALLERY.csv',
nyc_eatery_count_by_types = Counter(nyc_eatery_types)
daily_violations = defaultdict(int)
eateries_by_park = defaultdict(list)
nyc_eateries_parks:
eateries_by_park[park_id].append(name)
print(nyc_eatery_count_by_types.most_common(3))
csvfile = open('ART_GALLERY.csv',
csv.DictReader(csvfile):
print(row)
galleries_10310 = art_galleries.pop('10310')
types_of_cookies_eaten = set(cookies_eaten_today)
print(types_of_cookies_eaten)
cookies_jason_ate.difference(cookies_hugo_ate)
set(['oatmeal cream', 'peanut butter'])
Jason Myers
Instructor
Congratulations
DATA T YP E S F O R DATA S C I E N C E I NP Y T
HON