Skip to content

Instantly share code, notes, and snippets.

@bsweger
bsweger / useful_pandas_snippets.md
Last active April 19, 2024 18:04
Useful Pandas Snippets

Useful Pandas Snippets

A personal diary of DataFrame munging over the years.

Data Types and Conversion

Convert Series datatype to numeric (will error if column has non-numeric values)
(h/t @makmanalp)

@bsweger
bsweger / Vagrantfile
Created December 17, 2012 02:41
Vagrantfile to set up a Python virtual environment for data analysis
Vagrant::Config.run do |config|
config.vm.define :pythondata do |pythondata_config|
# Every Vagrant virtual environment requires a box to build off of.
pythondata_config.vm.box = "precise64"
# The url from where the 'config.vm.box' box will be fetched if it
# doesn't already exist on the user's system.
pythondata_config.vm.box_url = "http://files.vagrantup.com/precise64.box"
# Forward a port from the guest to the host, which allows for outside
@bsweger
bsweger / .bash_profile
Created July 6, 2012 19:32
.bash_profile - start ssh-agent
# adopted from https://confluence.atlassian.com/display/BITBUCKET/Set+up+SSH+for+Git#SetupSSHforGit-installpublickey, which was adopted from a GitHub script
if [ -r ~/.profile ]; then . ~/.profile; fi
case "$-" in *i*) if [ -r ~/.bashrc ]; then . ~/.bashrc; fi;; esac
echo
echo "Yo"
echo
SSH_ENV="$HOME/.ssh/environment"
@bsweger
bsweger / Spending Category.html
Created February 16, 2012 19:21
HTML Snippets for 2013 President's Budget
<script type="text/javascript" src="/media/uploads/publications/presidents_budget_fy2013/swfobject.js"></script>
<script type="text/javascript">
// For version detection, set to min. required Flash Player version, or 0 (or 0.0.0), for no version detection.
var swfVersionStr = "11.1.0";
// To use express install, set to playerProductInstall.swf, otherwise the empty string.
var xiSwfUrlStr = "/media/uploads/publications/presidents_budget_fy2013/playerProductInstall.swf";
var flashvars = {datafile: "/media/uploads/publications/presidents_budget_fy2013/info/budget-category.txt"}
var params = {};
params.quality = "high";
params.bgcolor = "#ffffff";
@bsweger
bsweger / pre-django-mysql.sql
Last active May 1, 2019 20:18
initial MySQL setup before hooking up a Django project
#create the database
CREATE DATABASE [dbname] CHARACTER SET utf8;
#create user
CREATE USER 'django_user'@'localhost' IDENTIFIED BY 'thepassword';
#give user permissions to db (probably not a great idea to GRANT ALL unless you really have to)
# (replace django.* with [mydatabasename].*)
GRANT ALL ON django.* TO 'django_user'@'localhost';
@bsweger
bsweger / django-startproject-win.py
Created August 28, 2011 23:59
create Django project in Windows 7 virtualenv
#Alternate to the django-admin.py startproject command.
#In windows, using 'django-admin.py startproject' is problematic...the .py extension
#is associated with the main install of Python, not the install on the current virtualenv, which fouls things up (e.g., import errors). Code below:
#1. forces module mode, which will check the path for django-admin (interpreter searches the path for modules but not for scripts)
#2. no more .py extension
#see http://stackoverflow.com/questions/2542674/no-matter-what-i-do-django-admin-py-is-not-found-even-though-its-in-my-path
-m django-admin startproject tutorial
@bsweger
bsweger / test-django-install.py
Created August 28, 2011 23:12
test django install
import django
django.VERSION
@bsweger
bsweger / ipython-virtualenv.py
Created August 27, 2011 23:58
make iPython virtual env aware (Windows)
#------------------------------------------------------------------------------
# Make things VirtualEnv aware (Windows version).
# More info: http://www.swegler.com/becky/blog/2011/08/28/python-django-mysql-on-windows-7-part-3-ipython-virtual-environments/
# add this to the end of ipython_config
# (or course, for virtualenvs created via --no-site-packages, it would
# be much easier just to install iPython)
#------------------------------------------------------------------------------
import sys
import site
from os import environ