Created
November 7, 2016 04:49
-
-
Save christlc/ee069dd98946b6d05a987e52481e9aa2 to your computer and use it in GitHub Desktop.
Convert a sub-range of excel to pandas dataframe in python
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
import os | |
import pandas as pd | |
from openpyxl import load_workbook | |
wb = load_workbook(filename='raw_data/A02.xlsx', | |
data_only=True) | |
ws = wb['A02e'] | |
def range_to_df(ws, remove_nan=True): | |
# Read the cell values into a list of lists | |
data_rows = [] | |
for row in ws: | |
data_cols = [] | |
for cell in row: | |
data_cols.append(cell.value) | |
data_rows.append(data_cols) | |
df = pd.DataFrame(data_rows[1:]) | |
df.columns = data_rows[0] | |
if remove_nan: | |
df.dropna(axis=1, how='all', inplace=True) | |
return df | |
ethnicity = range_to_df(ws['A6':'E13']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment