jack 测试Jupyter笔记本
测试Jupyter笔记本
test_jp_file.ipynb
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Series\n",
"Series is a one-dimensional labeled array capable of holding any data type (integers, strings, floating point numbers,\n",
"Python objects, etc.). The axis labels are collectively referred to as the index.\n",
"\n",
"documentation: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.html"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Create series from NumPy array\n",
"number of labels in 'index' must be the same as the number of elements in array"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"a 0.591872\n",
"b -1.381102\n",
"c 2.354536\n",
"d -1.339378\n",
"e -1.888910\n",
"dtype: float64"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_simple_series = pd.Series(np.random.randn(5), index=['a', 'b', 'c', 'd', 'e'])\n",
"my_simple_series"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Index(['a', 'b', 'c', 'd', 'e'], dtype='object')"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_simple_series.index # print all the index associated with a series object"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Create series from NumPy array, without explicit index"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 -0.839923\n",
"1 0.310805\n",
"2 -0.491671\n",
"3 -0.418519\n",
"4 -1.785353\n",
"dtype: float64"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_simple_series = pd.Series(np.random.randn(5))\n",
"my_simple_series"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"### Access a series like a NumPy array"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 -0.839923\n",
"1 0.310805\n",
"2 -0.491671\n",
"dtype: float64"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_simple_series[:3] # start from 0 and move 3 positions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Create series from Python dictionary"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"a 45.0\n",
"b -19.5\n",
"c 4444.0\n",
"dtype: float64"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_dictionary = {'a' : 45., 'b' : -19.5, 'c' : 4444}\n",
"my_second_series = pd.Series(my_dictionary)\n",
"my_second_series"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"### Access series elements like a dictionary"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-19.5"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_second_series['b']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**note order in display; same as order in \"index\"**\n",
"\n",
"**note NaN**"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"b -19.5\n",
"c 4444.0\n",
"d NaN\n",
"a 45.0\n",
"dtype: float64"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.Series(my_dictionary, index=['b', 'c', 'd', 'a'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Using the get method"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"45.0"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_second_series.get('a')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Trying to fetch value of element whose index does not exist**"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"NoneType"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"unknown = my_second_series.get('f')\n",
"type(unknown)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Create series from scalar\n",
"If data is a scalar value, an index must be provided. The value will be repeated to match the length of index"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"a 5.0\n",
"b 5.0\n",
"c 5.0\n",
"d 5.0\n",
"e 5.0\n",
"dtype: float64"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.Series(5., index=['a', 'b', 'c', 'd', 'e'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.6.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}