{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 2, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
namesexagefare
0Allen, Miss. Elisabeth Waltonfemale29.0000211.3375
1Allison, Master. Hudson Trevormale0.9167151.5500
2Allison, Miss. Helen Lorainefemale2.0000151.5500
3Allison, Mr. Hudson Joshua Creightonmale30.0000151.5500
4Allison, Mrs. Hudson J C (Bessie Waldo Daniels)female25.0000151.5500
5Anderson, Mr. Harrymale48.000026.5500
\n", "
" ], "text/plain": [ " name sex age fare\n", "0 Allen, Miss. Elisabeth Walton female 29.0000 211.3375\n", "1 Allison, Master. Hudson Trevor male 0.9167 151.5500\n", "2 Allison, Miss. Helen Loraine female 2.0000 151.5500\n", "3 Allison, Mr. Hudson Joshua Creighton male 30.0000 151.5500\n", "4 Allison, Mrs. Hudson J C (Bessie Waldo Daniels) female 25.0000 151.5500\n", "5 Anderson, Mr. Harry male 48.0000 26.5500" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Read the csv file\n", "titanic_df = pd.read_csv('titanic.csv')\n", "\n", "#It's a big file so let's extract a small data out of it\n", "df = titanic_df.loc[[0,1,2,3,4,5],['name','sex','age','fare']]\n", "df" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 Allen, Miss. Elisabeth Walton\n", "1 Allison, Master. Hudson Trevor\n", "2 Allison, Miss. Helen Loraine\n", "3 Allison, Mr. Hudson Joshua Creighton\n", "4 Allison, Mrs. Hudson J C (Bessie Waldo Daniels)\n", "5 Anderson, Mr. Harry\n", "Name: name, dtype: object" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Let's just print the name\n", "df.name" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 Allen, Miss. Elisabeth Walton\n", "1 Allison, Master. Hudson Trevor\n", "2 Allison, Miss. Helen Loraine\n", "3 Allison, Mr. Hudson Joshua Creighton\n", "4 Allison, Mrs. Hudson J C (Bessie Waldo Daniels)\n", "5 Anderson, Mr. Harry\n", "Name: name, dtype: object" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#we can print the name using loc also\n", "df.loc[:,'name']" ] }, { "cell_type": "code", "execution_count": 5, "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", "
namesexagefare
0Allen, Miss. Elisabeth Waltonfemale29.0000211.3375
1Allison, Master. Hudson Trevormale0.9167151.5500
\n", "
" ], "text/plain": [ " name sex age fare\n", "0 Allen, Miss. Elisabeth Walton female 29.0000 211.3375\n", "1 Allison, Master. Hudson Trevor male 0.9167 151.5500" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#iloc can be used with positional integers\n", "#First two rows and all columns\n", "df.iloc[0:2,:]" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 211.3375\n", "1 151.5500\n", "2 151.5500\n", "3 151.5500\n", "4 151.5500\n", "5 26.5500\n", "Name: fare, dtype: float64" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#all rows and last column\n", "df.iloc[:,-1]" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Int64Index([0, 1, 2, 3, 4, 5], dtype='int64')" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#show the index\n", "df.index" ] }, { "cell_type": "code", "execution_count": 8, "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", "
namesexagefare
0Allen, Miss. Elisabeth Waltonfemale29.0211.3375
2Allison, Miss. Helen Lorainefemale2.0151.5500
4Allison, Mrs. Hudson J C (Bessie Waldo Daniels)female25.0151.5500
\n", "
" ], "text/plain": [ " name sex age fare\n", "0 Allen, Miss. Elisabeth Walton female 29.0 211.3375\n", "2 Allison, Miss. Helen Loraine female 2.0 151.5500\n", "4 Allison, Mrs. Hudson J C (Bessie Waldo Daniels) female 25.0 151.5500" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#using criteria to filter\n", "df[df.sex == 'female']" ] }, { "cell_type": "code", "execution_count": 9, "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", "
namesexagefare
2Allison, Miss. Helen Lorainefemale2.0151.55
\n", "
" ], "text/plain": [ " name sex age fare\n", "2 Allison, Miss. Helen Loraine female 2.0 151.55" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df[df.index == 2]" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "scrolled": true }, "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", "
namesexagefare
0Allen, Miss. Elisabeth Waltonfemale29.0211.3375
4Allison, Mrs. Hudson J C (Bessie Waldo Daniels)female25.0151.5500
\n", "
" ], "text/plain": [ " name sex age fare\n", "0 Allen, Miss. Elisabeth Walton female 29.0 211.3375\n", "4 Allison, Mrs. Hudson J C (Bessie Waldo Daniels) female 25.0 151.5500" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#we can use conditions\n", "df[(df.sex == 'female') & (df.age >= 20)]" ] } ], "metadata": { "celltoolbar": "Raw Cell Format", "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 }