Created
November 20, 2020 16:52
-
-
Save nickinack/63714655b8900c3d96c8c59f52946f55 to your computer and use it in GitHub Desktop.
This file contains 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
{ | |
"metadata": { | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": 3 | |
}, | |
"orig_nbformat": 2 | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2, | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"df1 = pd.read_csv(\"communities.data\" , header=None)\n", | |
"def read_header(filename):\n", | |
" '''\n", | |
" Given a filename containing headers, extract the headers and assign it to df\n", | |
" '''\n", | |
"\n", | |
" header_list = []\n", | |
" with open(filename) as f:\n", | |
" for line in f:\n", | |
" if \"@attribute\" in line:\n", | |
" header_list.append(line.split(\" \")[1])\n", | |
" return header_list\n", | |
" \n", | |
"headers = read_header(\"communities.names\")\n", | |
"df1.columns = headers" | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment