{ "cells": [ { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "data = pd.Series([1,2,3,4,5])\n", "print(type(data))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(numpy.ndarray, array([1, 2, 3, 4, 5], dtype=int64))" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(data.values), data.values" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(pandas.core.indexes.range.RangeIndex, RangeIndex(start=0, stop=5, step=1))" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(data.index), data.index" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "one 5\n", "two 4\n", "three 6\n", "four 3\n", "five 1\n", "dtype: int64" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data=pd.Series([5,4,6,3,1],index=['one','two','three','four','five'])\n", "data" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'b', 'c', 'd', 'e']" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list('abcde')" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "bj 3000\n", "gz 1500\n", "sh 2800\n", "sz 1200\n", "dtype: int64" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "population_dict={'bj':3000,'gz':1500,'sh':2800,'sz':1200}\n", "population_series=pd.Series(population_dict)\n", "population_series" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "bj 3000\n", "sh 2800\n", "dtype: int64" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sub_series=pd.Series(population_dict,index=['bj','sh']) #如果存在取交集\n", "sub_series" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "bj 3000.0\n", "xa NaN\n", "dtype: float64" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sub_series=pd.Series(population_dict,index=['bj','xa']) #如果不存在则值为NaN\n", "sub_series" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4 10\n", "3 10\n", "2 10\n", "5 10\n", "dtype: int64" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data=pd.Series(10,index=[4,3,2,5])\n", "data" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
areapopulation
beijing3003000
guangzhou2001800
shanghai1801200
\n", "
" ], "text/plain": [ " area population\n", "beijing 300 3000\n", "guangzhou 200 1800\n", "shanghai 180 1200" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "population_dict={'beijing':3000,'shanghai':1200,'guangzhou':1800}\n", "area_dict={'beijing':300,'guangzhou':200,'shanghai':180}\n", "population_series=pd.Series(population_dict)\n", "area_series=pd.Series(area_dict)\n", "citys=pd.DataFrame({'area':area_series,'population':population_series})\n", "citys" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(numpy.ndarray, (3, 2), array([[ 300, 3000],\n", " [ 200, 1800],\n", " [ 180, 1200]], dtype=int64))" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(citys.values) ,citys.values.shape, citys.values" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(pandas.core.indexes.base.Index,\n", " Index(['beijing', 'guangzhou', 'shanghai'], dtype='object'))" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(citys.index), citys.index" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(pandas.core.indexes.base.Index, Index(['area', 'population'], dtype='object'))" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(citys.columns), citys.columns" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
beijingguangzhoushanghai
0300018001200
1300200180
\n", "
" ], "text/plain": [ " beijing guangzhou shanghai\n", "0 3000 1800 1200\n", "1 300 200 180" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "population_dict={'beijing':3000,'shanghai':1200,'guangzhou':1800}\n", "area_dict={'beijing':300,'shanghai':180,'guangzhou':200}\n", "data=pd.DataFrame([population_dict,area_dict])\n", "data" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
beijingguangzhoushanghai
population300018001200
area300200180
\n", "
" ], "text/plain": [ " beijing guangzhou shanghai\n", "population 3000 1800 1200\n", "area 300 200 180" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "population_dict={'beijing':3000,'shanghai':1200,'guangzhou':1800}\n", "area_dict={'beijing':300,'shanghai':180,'guangzhou':200}\n", "data=pd.DataFrame([population_dict,area_dict], index=['population','area'])\n", "data" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
population
beijing3000
guangzhou1800
shanghai1200
\n", "
" ], "text/plain": [ " population\n", "beijing 3000\n", "guangzhou 1800\n", "shanghai 1200" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "population_series=pd.Series(population_dict)\n", "# pd.DataFrame(population_series,columns=['population'])\n", "pd.DataFrame({'population':population_series})" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ab
一00
二12
三24
\n", "
" ], "text/plain": [ " a b\n", "一 0 0\n", "二 1 2\n", "三 2 4" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.DataFrame([{'a':i,'b':i*2} for i in range(3)], index=['一','二','三'])" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
abc
a885918
b359579
c182618
d127063
\n", "
" ], "text/plain": [ " a b c\n", "a 88 59 18\n", "b 35 95 79\n", "c 18 26 18\n", "d 12 70 63" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd.DataFrame(np.random.randint(0, 100, (4,3)), index=['a','b','c','d'], columns=['a','b','c'])" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'根据key获取:'" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "4" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "'切片获取:'" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "a 4\n", "b 3\n", "c 25\n", "d 2\n", "dtype: int64" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "'索引获取:'" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "3" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "'索引切片:'" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "c 25\n", "d 2\n", "dtype: int64" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "data=pd.Series([4,3,25,2,3],index=list('abcde'))\n", "display('根据key获取:',data['a'])\n", "display('切片获取:',data['a':'d'])\n", "display('索引获取:',data[1])\n", "display('索引切片:',data[2:4])" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data=pd.Series([5,6,7,8],index=[1,2,3,4])\n", "data[1] " ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.loc[1]" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.iloc[1]" ] } ], "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.3" } }, "nbformat": 4, "nbformat_minor": 2 }