Last active
May 6, 2021 01:50
-
-
Save pbeens/d7207d23fa98f127e7bc75ec6b7b407f to your computer and use it in GitHub Desktop.
Lists to CSV Converter 2.ipynb
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
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "Lists to CSV Converter 2.ipynb", | |
"provenance": [], | |
"collapsed_sections": [], | |
"authorship_tag": "ABX9TyNrSNaeP4quk9UDJY7UmwmZ", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/pbeens/d7207d23fa98f127e7bc75ec6b7b407f/lists-to-csv-converter.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "55fTccsERlS0" | |
}, | |
"source": [ | |
"This utility will convert a series of lists into a CSV string for pasting into your favourite application. \n", | |
"\n", | |
"It utilizes Pandas for speed, so this will work particularly well if you have large lists of data.\n", | |
"\n", | |
"Be sure the length of each list is the same." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "QQoEHALBC1an" | |
}, | |
"source": [ | |
"import pandas as pd" | |
], | |
"execution_count": 1, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "GhhBUAJS-WaZ" | |
}, | |
"source": [ | |
"# initialize lists and an empty dict\n", | |
"\n", | |
"x = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n", | |
"y1 = [1, 3, 5, 3, 1, 3, 5, 3, 1]\n", | |
"y2 = [2, 4, 6, 4, 2, 4, 6, 4, 2]\n", | |
"\n", | |
"d = {}" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 195 | |
}, | |
"id": "thhWPTNVDEib", | |
"outputId": "3b5a6cd7-5369-47b1-a3f8-68719bf9d74c" | |
}, | |
"source": [ | |
"# put lists in dict\n", | |
"\n", | |
"d['x'] = x\n", | |
"d['y1'] = y1\n", | |
"d['y2'] = y2\n", | |
"\n", | |
"# create dataframe\n", | |
"df = pd.DataFrame(d) \n", | |
"\n", | |
"# warm fuzzy feeling\n", | |
"df.head()" | |
], | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>x</th>\n", | |
" <th>y1</th>\n", | |
" <th>y2</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>1</td>\n", | |
" <td>1</td>\n", | |
" <td>2</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>2</td>\n", | |
" <td>3</td>\n", | |
" <td>4</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>3</td>\n", | |
" <td>5</td>\n", | |
" <td>6</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>4</td>\n", | |
" <td>3</td>\n", | |
" <td>4</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>5</td>\n", | |
" <td>1</td>\n", | |
" <td>2</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" x y1 y2\n", | |
"0 1 1 2\n", | |
"1 2 3 4\n", | |
"2 3 5 6\n", | |
"3 4 3 4\n", | |
"4 5 1 2" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 3 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"id": "W-Wv77td_oF7", | |
"outputId": "abc83d57-f5d1-4097-f8c8-50409dd92759" | |
}, | |
"source": [ | |
"df.to_csv(index=False)\n", | |
"\n", | |
"# copy the output and paste into Google Sheets or Gist or..." | |
], | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"application/vnd.google.colaboratory.intrinsic+json": { | |
"type": "string" | |
}, | |
"text/plain": [ | |
"'x,y1,y2\\n1,1,2\\n2,3,4\\n3,5,6\\n4,3,4\\n5,1,2\\n6,3,4\\n7,5,6\\n8,3,4\\n9,1,2\\n'" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 4 | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "kNOS7uazRGSy" | |
}, | |
"source": [ | |
"If pasting into Google Sheets, choose the **Split text to columns** option.\n", | |
"\n", | |
"\n", | |
"" | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment