Created
November 1, 2017 21:12
-
-
Save abehmiel/cfe4bc3ae5222b8f046b0f32d5f35a4b to your computer and use it in GitHub Desktop.
Convert tabular pdf data to a csv and also read it as a python dataframe
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
# It's really stupid when the gov't releases pdf's of tabular data. So I made a quick, hacky script to | |
# fix their mistakes for them. (I'm referring to https://t.co/oOyhHNVvjS ) | |
# requirements: | |
# pandas | |
# tabula-py | |
import pandas as pd | |
from tabula import read_pdf | |
# read the pdf-- it's all messed up and only one space-delimited column. | |
# also it defauls to only loading one page unless you specify pages='all' or | |
# a different int or list. | |
df = read_pdf("exhibit_b.pdf", pages='all') | |
# fix the columns | |
df['user id'] = df['user id handle'].apply(lambda x: x.split()[0]) | |
df['handle'] = df['user id handle'].apply(lambda x: x.split()[1]) | |
df = df.drop('user id handle', axis=1) | |
# output to csv | |
df.to_csv('exhibit_b.csv', index=False) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment