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 | |
import random | |
import time | |
# creating fake dataset | |
sample_size = 10000 | |
cat1_pop = range(1, 2001) | |
cat2_pop = ['a', 'b', 'c', 'd', 'e'] | |
cat1_vals = [random.choice(cat1_pop) for _ in range(sample_size)] | |
cat2_vals = [random.choice(cat2_pop) for _ in range(len(cat1_pop))] |
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 numpy | |
import pandas | |
import matplotlib.pyplot as plt | |
import seaborn as sns | |
from ggplot import * | |
plt.style.use('ggplot') | |
def get_histogram_xy(data, bins=10): | |
"""Returns x,y coordinates for Histogram data. |
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 | |
def map_columns(df=None, col1=None, col2=None): | |
""" | |
Returns a mapping dictionary based on two columns: | |
key = col1 values | |
value = col2 values | |
Mapping as a dict is useful only as long as we have unique values for keys. | |
""" |
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
""" | |
Use: | |
>>> pandas.DataFrame.mask = mask | |
>>> df.mask('Col1', 'is', value).mask('Col3', 'not', value) | |
""" | |
import pandas | |
def mask(df, key=None, op='is', val=None): | |
if op == 'is': |
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 xlrd | |
import csv | |
import os | |
def csv_from_xlsx(xlfile): | |
""" | |
Convert Microsoft Excel File .xls(x) to csv | |
""" | |
workbook = xlrd.open_workbook(xlfile) | |
sheet = workbook.sheet_by_index(0) |
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 IPython.display import HTML | |
HTML('''<script> | |
code_show=true; | |
function code_toggle() { | |
if (code_show){ | |
$('div.input').hide(); | |
} else { | |
$('div.input').show(); | |
} |
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 json | |
import argparse | |
def generate_py(ipynb_file, op_file): | |
# get data | |
with open(ipynb_file) as ipynb: | |
data = json.load(ipynb) | |
# check language in metadata | |
if data['metadata']['kernelspec']['language'] != 'python': | |
print 'Warning. Language not python.' |