Skip to content

Instantly share code, notes, and snippets.

Becky Sweger bsweger

View GitHub Profile
import pytest
import requests
def mock_request_json():
"""Return fake API data."""
return {'spring': [
'name': 'birds',
'status': 'chirpin',
View gist:24a47e4c253dc9b17869ef096700dffd
### Keybase proof
I hereby claim:
* I am bsweger on github.
* I am bendystraw ( on keybase.
* I have a public key ASCmomBSZgzxe3w1YQf-eZwTB3KNg7k4VGD29NN8hhmrDAo
To claim this, I am signing this object:
bsweger /
Created Mar 9, 2018
USASpending API Snippets
# Get SETI Institute grants from USAspending API into a pandas dataframe
import json
import requests
import pandas as pd
from import json_normalize
uri = ''
headers = {'content-type': 'application/json'}
seti_json = []
next = 1
bsweger / iterm_profile.json
Created Dec 12, 2017
Profiles and Dotfiles
View iterm_profile.json
"Ansi 3 Color" : {
"Green Component" : 0.73333334922790527,
"Blue Component" : 0,
"Red Component" : 0.73333334922790527
"Tags" : [
"Ansi 12 Color" : {
bsweger /
Last active Jun 7, 2017

Retrieve financial balances by agency and fiscal year

Route: /api/v2/financial_balances/agencies

Method: GET

This route retrieves financial balance information by funding agency and fiscal year

Sample Request


bsweger /
Created Jul 13, 2016
Apply a padding function to .csv columns (Pandas)
# example of using a parameterized function as a converter when reading .csv in pandas
import pandas as pd
# a function that will be used to pad datafram column values to a specified length
# (some incoming values are multiple spaces; those should convert to Noe)
padFunction = lambda field, padTo: str(field).strip().zfill(padTo) if len(str(field).strip()) else None
# read file w/o using converters and display list of unique alloc_id values
pa = pd.read_csv(
bsweger /
Last active Nov 26, 2019
Convert markdown table rows to html (we converted some markdown tables to html so we could use the "scope" attribute in the header rows for accessibility).
#tabedata.txt is a file that contains the piped markdown table rows
with open ('markdownrows.txt') as f:
content = f.readlines()
rows = []
for c in content:
cells = c.split('|')
cells = ['<td>{}</td>'.format(cell.strip()) for cell in cells]
View Taxes Paid and Personal Income.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

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 / Vagrantfile
Created Dec 17, 2012
Vagrantfile to set up a Python virtual environment for data analysis
View Vagrantfile do |config|
config.vm.define :pythondata do |pythondata_config|
# Every Vagrant virtual environment requires a box to build off of. = "precise64"
# The url from where the '' box will be fetched if it
# doesn't already exist on the user's system.
pythondata_config.vm.box_url = ""
# Forward a port from the guest to the host, which allows for outside
You can’t perform that action at this time.