This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from itertools import izip_longest | |
| names = ['Karl','Savannah', 'Harvey', 'Scarlett'] | |
| letters = [len(n) for n in names] | |
| print " --- merge even lists ---" | |
| for name, count in izip_longest(names, letters): | |
| print "Name '{0}' has {1} letters".format(name, count) | |
| print " --- merge uneven lists ---" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from functools import partial | |
| def raise_to_power(value, power): | |
| return value ** power | |
| square = partial(raise_to_power, power=2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from os.path import isfile | |
| if isfile('filename.xxx'): | |
| print 'file exists' | |
| else: | |
| print 'no file exists' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pendulum | |
| # pip install pendulum | |
| if __name__ == '__main__': | |
| _1st_jan_2011 = pendulum.create(day=1, month=1, year=2011) | |
| print _1st_jan_2011.days_in_month |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pandas as pd | |
| df = pd.DataFrame(data = [1,2,3,4,5], index = pd.date_range("7:00", "9:00", freq="30min", tz = 'UTC')) | |
| # convert UTC Timezone to Australia Perth | |
| df.index = df.index.tz_convert('Australia/Perth') | |
| # Remove TZ information from the datetime stamp, this is useful when exporting to Excel | |
| df.index = df.index.tz_localize(None) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from pytz import all_timezones | |
| print all_timezones |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pandas as pd | |
| from datetime import datetime | |
| dd = 01 | |
| mm = 01 | |
| yyyy = 2011 | |
| pd.set_option('display.expand_frame_repr', False) | |
| print '------ Day Shift ---------' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pendulum | |
| # pip install pendulum | |
| if __name__ == '__main__': | |
| _1st_day = pendulum.now().subtract(months=1).start_of('month') | |
| last_day = pendulum.now().subtract(months=1).end_of('month') | |
| print 'First day of previous month: {}'.format(_1st_day.to_date_string()) | |
| print 'Last day of previous month: {}'.format(last_day.to_date_string()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import multiprocessing | |
| def increment_value(num): | |
| num = num + 1 | |
| print 'this is number ->{}'.format(num) | |
| return num | |
| if __name__ == '__main__': | |
| ''' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from collections import Counter | |
| word_count = Counter('my my name is is is Hal hal'.split()) | |
| for word, count in word_count.items(): | |
| print 'Word Count: {} -> {}'.format(count, word) |
OlderNewer