Skip to content

Instantly share code, notes, and snippets.

View chankeypathak's full-sized avatar
Commit all day, everyday!

Chankey Pathak chankeypathak

Commit all day, everyday!
View GitHub Profile
@chankeypathak
chankeypathak / py_snip.py
Created June 19, 2017 06:21
Python snippets
if any(bad in name for bad in NOT_ALLOWED):
raise ValueError("'name' may not contain any of {}".format(NOT_ALLOWED)
if not all(isinstance(val, int) for val in DATA) or len(DATA) !=2:
raise ValueError("'DATA' must consist of 2 ints")
# List unique values in a DataFrame column
pd.unique(df.column_name.ravel())
# Convert Series datatype to numeric, getting rid of any non-numeric values
df['col'] = df['col'].astype(str).convert_objects(convert_numeric=True)
# Grab DataFrame rows where column has certain values
valuelist = ['value1', 'value2', 'value3']
df = df[df.column.isin(valuelist)]
@chankeypathak
chankeypathak / pandas_concat_and_axis.py
Created June 5, 2017 09:09
Concatenating 2 pandas series objects with axis 1
import pandas as pd
sodium = [ 7.22, 30.07 ]
sodium_index = [ '2017-06-01', '2017-06-02' ]
potassium = [12.19, 10.07]
potassium_index = [ '2017-06-01', '2017-06-02' ]
sodium_series = pd.Series(sodium, index=sodium_index)
potassium_series = pd.Series(potassium, index=potassium_index)
@chankeypathak
chankeypathak / pandas_matplot_txt.py
Created May 8, 2017 10:17
Reading from TXT - Exporting to TXT - Selecting top/bottom records - Descriptive statistics - Grouping/sorting data
import pandas as pd
import matplotlib.pyplot as plt
from numpy import random
import os
# The inital set of baby names
names = ['Bob','Jessica','Mary','John','Mel']
# This will ensure the random samples below can be reproduced.
# This means the random samples will always be identical.
@chankeypathak
chankeypathak / pandas_matplot_csv.py
Last active May 8, 2017 10:17
matplotlib basic example: Importing libraries - Creating data sets - Creating data frames - Reading from CSV - Exporting to CSV - Finding maximums - Plotting data
import pandas as pd #this is how I usually import pandas
import matplotlib.pyplot as plt
names = ['Bob','Jessica','Mary','John','Mel']
births = [968, 155, 77, 578, 973]
BabyDataSet = list(zip(names,births))
df = pd.DataFrame(data = BabyDataSet, columns=['Names', 'Births'])
# Create graph
df['Births'].plot()
@chankeypathak
chankeypathak / sqlite_class.py
Created April 27, 2017 08:26
Generic SQLite class in Python
class SqliteDB:
def __init__(self):
self.parent_directory = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
self.db_path = os.path.join(self.parent_directory, 'db.sqlite3')
self.db_connection = sqlite3.connect(self.db_path)
self.db_cursor = self.db_connection.cursor()
def execute(self, query, args=''):
return self.db_cursor.execute(query, args)
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
#author: rex
#blog: http://iregex.org
#filename trie.py
#created: 2010-08-01 20:24
#source uri: http://iregex.org/blog/trie-in-python.html
# escape bug fix by fcicq @ 2012.8.19
@chankeypathak
chankeypathak / sort_date.pl
Created November 29, 2016 07:46
Sort dates using Schwartzian transform in Perl
#!/usr/bin/perl
use strict;
use warnings;
use Time::Piece;
my @events = ( '11/17/1999', '12/6/1999', '12/23/1999',
'1/23/2000', '1/13/2000', '2/25/2000',
'1/5/2000', '3/18/2000', '4/10/2000',
'3/12/2000', '12/31/1999');
@chankeypathak
chankeypathak / tokeparser.pl
Created September 18, 2016 10:38
HTML::TokeParser::Simple Example
use utf8;
use strict;
use warnings;
use open qw[ :std :encoding(UTF-8) ];
use HTML::TokeParser::Simple;
run(\@ARGV);
sub run {
@chankeypathak
chankeypathak / win32ole.pl
Last active November 18, 2016 05:32
Win32::OLE examples
#!/usr/bin/perl
#Author: Chankey Pathak
#Date: 29 Sept 2015
#TODO: process files of entire directory
use strict;
use warnings;
use Data::Dump qw(dump);
use Getopt::Long;
use Win32::OLE qw(valof);
use PDF::API2;