{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": 3, "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", "
ABCD
a0123
b4567
c891011
\n", "
" ], "text/plain": [ " A B C D\n", "a 0 1 2 3\n", "b 4 5 6 7\n", "c 8 9 10 11" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data=pd.DataFrame(np.arange(12).reshape(3,4),index=list('abc'),columns=list('ABCD'))\n", "data" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(a 1\n", " b 5\n", " c 9\n", " Name: B, dtype: int32, pandas.core.series.Series)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data['B'], type(data['B'])" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "( A B\n", " a 0 1\n", " b 4 5\n", " c 8 9, pandas.core.frame.DataFrame)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data[['A','B']], type(data[['A','B']])" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "a 0\n", "b 4\n", "c 8\n", "Name: A, dtype: int32" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.iloc[:,0]" ] }, { "cell_type": "code", "execution_count": 11, "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", "
AC
a02
b46
c810
\n", "
" ], "text/plain": [ " A C\n", "a 0 2\n", "b 4 6\n", "c 8 10" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.iloc[:,[0,2]]" ] }, { "cell_type": "code", "execution_count": 12, "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
a01
b45
c89
\n", "
" ], "text/plain": [ " A B\n", "a 0 1\n", "b 4 5\n", "c 8 9" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.iloc[:,0:2]" ] }, { "cell_type": "code", "execution_count": 13, "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", "
ABC
a012
b456
c8910
\n", "
" ], "text/plain": [ " A B C\n", "a 0 1 2\n", "b 4 5 6\n", "c 8 9 10" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.loc[:, 'A':'C']" ] }, { "cell_type": "code", "execution_count": 14, "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", "
AB
a01
b45
\n", "
" ], "text/plain": [ " A B\n", "a 0 1\n", "b 4 5" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.loc['a':'b','A':'B']" ] }, { "cell_type": "code", "execution_count": 15, "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", "
AB
a01
b45
\n", "
" ], "text/plain": [ " A B\n", "a 0 1\n", "b 4 5" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.iloc[0:2, 0:2]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "print(data.loc[['a','c'],['A','B','D']])" ] } ], "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 }