Skip to content

Instantly share code, notes, and snippets.

@twiecki
Created March 7, 2013 22:27
Show Gist options
  • Save twiecki/5112405 to your computer and use it in GitHub Desktop.
Save twiecki/5112405 to your computer and use it in GitHub Desktop.
{
"metadata": {
"name": "csv_conversion"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"import pandas as pd\n",
"from StringIO import StringIO\n",
"import datetime"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 10
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"csv_str = 'Date,AMD,CERN,COST,DELL,GPS,INTC,MMM\\n2001-08-01 00:00:00+00:00,18.899999999999999,13.51,35.700000000000003,26.75,23.420000000000002,24.23,41.880000000000003\\n2001-08-02 00:00:00+00:00,19.760000000000002,13.470000000000001,36.670000000000002,27.98,23.550000000000001,25.300000000000001,41.990000000000002\\n2001-08-03 00:00:00+00:00,19.25,13.44,36.350000000000001,27.629999999999999,23.25,24.969999999999999,42.159999999999997'"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print csv_str"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Date,AMD,CERN,COST,DELL,GPS,INTC,MMM\n",
"2001-08-01 00:00:00+00:00,18.899999999999999,13.51,35.700000000000003,26.75,23.420000000000002,24.23,41.880000000000003\n",
"2001-08-02 00:00:00+00:00,19.760000000000002,13.470000000000001,36.670000000000002,27.98,23.550000000000001,25.300000000000001,41.990000000000002\n",
"2001-08-03 00:00:00+00:00,19.25,13.44,36.350000000000001,27.629999999999999,23.25,24.969999999999999,42.159999999999997\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"data = pd.DataFrame.from_csv(StringIO(csv_str))"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 12
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"data"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>AMD</th>\n",
" <th>CERN</th>\n",
" <th>COST</th>\n",
" <th>DELL</th>\n",
" <th>GPS</th>\n",
" <th>INTC</th>\n",
" <th>MMM</th>\n",
" </tr>\n",
" <tr>\n",
" <th>Date</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <td><strong>2001-08-01 00:00:00+00:00</strong></td>\n",
" <td> 18.90</td>\n",
" <td> 13.51</td>\n",
" <td> 35.70</td>\n",
" <td> 26.75</td>\n",
" <td> 23.42</td>\n",
" <td> 24.23</td>\n",
" <td> 41.88</td>\n",
" </tr>\n",
" <tr>\n",
" <td><strong>2001-08-02 00:00:00+00:00</strong></td>\n",
" <td> 19.76</td>\n",
" <td> 13.47</td>\n",
" <td> 36.67</td>\n",
" <td> 27.98</td>\n",
" <td> 23.55</td>\n",
" <td> 25.30</td>\n",
" <td> 41.99</td>\n",
" </tr>\n",
" <tr>\n",
" <td><strong>2001-08-03 00:00:00+00:00</strong></td>\n",
" <td> 19.25</td>\n",
" <td> 13.44</td>\n",
" <td> 36.35</td>\n",
" <td> 27.63</td>\n",
" <td> 23.25</td>\n",
" <td> 24.97</td>\n",
" <td> 42.16</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"output_type": "pyout",
"prompt_number": 13,
"text": [
" AMD CERN COST DELL GPS INTC MMM\n",
"Date \n",
"2001-08-01 00:00:00+00:00 18.90 13.51 35.70 26.75 23.42 24.23 41.88\n",
"2001-08-02 00:00:00+00:00 19.76 13.47 36.67 27.98 23.55 25.30 41.99\n",
"2001-08-03 00:00:00+00:00 19.25 13.44 36.35 27.63 23.25 24.97 42.16"
]
}
],
"prompt_number": 13
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"incoming_dt = datetime.datetime(year=2001, month=8, day=1)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 11
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"data.ix[incoming_dt]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 14,
"text": [
"AMD 18.90\n",
"CERN 13.51\n",
"COST 35.70\n",
"DELL 26.75\n",
"GPS 23.42\n",
"INTC 24.23\n",
"MMM 41.88\n",
"Name: 2001-08-01 00:00:00+00:00"
]
}
],
"prompt_number": 14
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment