Skip to content

Instantly share code, notes, and snippets.

View vignesh-saptarishi's full-sized avatar

Vignesh Saptarishi Ramesh vignesh-saptarishi

View GitHub Profile
@vignesh-saptarishi
vignesh-saptarishi / pandas_apply_map_compare.py
Created February 23, 2017 09:37
Comparing pandas methods in applying a function
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))]
@vignesh-saptarishi
vignesh-saptarishi / mpl_pandas_plot_tools.py
Created February 23, 2017 09:23
Utility functions for visualization using pandas dataframes and matplotlib
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.
@vignesh-saptarishi
vignesh-saptarishi / pandas_map_merge_cols.py
Created February 23, 2017 09:18
Map and merge columns across dataframes
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.
"""
@vignesh-saptarishi
vignesh-saptarishi / pandas_chain_mask.py
Created February 23, 2017 09:15
Chaining pandas mask operation
"""
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':
@vignesh-saptarishi
vignesh-saptarishi / xl2csv.py
Created February 23, 2017 09:04
Convert xls(x) file to csv
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)
@vignesh-saptarishi
vignesh-saptarishi / ipynb_toggle_code.py
Created February 23, 2017 09:02
Toggle show python code in HTML generated from ipynb
from IPython.display import HTML
HTML('''<script>
code_show=true;
function code_toggle() {
if (code_show){
$('div.input').hide();
} else {
$('div.input').show();
}
@vignesh-saptarishi
vignesh-saptarishi / generate_py_from_ipynb.py
Last active February 23, 2017 09:24
Generate python file from ipynb file
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.'