{"cells":[{"metadata":{"_uuid":"43f7ead10550fc82e3218c7887eb67586f68ea8b"},"cell_type":"markdown","source":"<h2 style=\"color:Blue;\"> Advance Pandas Trick and Techniques </h2>\n\n----\n\n<h3 style=\"color:Blue;\">Index of learning</h3>  <a id=\"00\"> </a>\n\n\n---\n1. [**Basic Pandas Reading Method**](#1)\n1. [**Advance Pandas Reading Methods**](#2)\n    1. [**Manipulating Column & Index Locations and Names**](#21)\n    1. [**Data Parsing options**](#22)\n    1. [**Reading data from excel files**](#23)\n    1. [**Reading data from some other popular formats**](#24)\n1. [**Apply multiple filter criteria to a pandas DataFrame**](#3)\n1. [**Changing the datatype of a Pandas Series**](#4)\n1. [**Filter rows of a pandas DataFrame by column value**](#5)\n1. [**Selecting multiple rows and columns from a pandas DataFrame**](#6)\n1. [**Sorting a pandas DataFrame or a Series**](#7)\n1. [**Using pandas Series data structure to select a subset of the data**](#8)\n1. [**Using string methods in pandas**](#9)\n1. [**Using the axis parameter in pandas**](#10)\n1. [**Applying a function to a pandas Series or DataFrame** ](#11)\n1. [**Handling SettingWithCopyWarning**](#12)\n1. [**Handling missing values in pandas**](#13)\n1. [**Indexing in pandas dataframes**](#14)\n1. [**Merging and concatenating multiple data frames into one** ](#15)\n1. [**Modifying a Pandas Dataframe inplace**](#16)\n1. [**Removing columns from a pandas DataFrame**](#17)\n1. [**Renaming columns in a pandas DataFrame**](#18)\n1. [**Using groupby method**](#19)\n1. [**Work with dates and times data**](#20)\n1. [**Choosing the colors for the plots**](#211)\n1. [**Controlling plot aesthetics**](#221)\n1. [**Plotting categorical data**](#231)\n1. [**Plotting with data aware grids**](#241)\n \n---\n"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"%%html\n<style>\n.output_wrapper, .output {\n    height:auto !important;\n    max-height:350px;  /* your desired max-height here */\n}\n.output_scroll {\n    box-shadow:none !important;\n    webkit-box-shadow:none !important;\n}\n</style>","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true,"_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"from IPython.core.interactiveshell import InteractiveShell\nInteractiveShell.ast_node_interactivity = \"all\"","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"906802c5feb7d6426f38964bfe19992f20a1a75f"},"cell_type":"markdown","source":"# 1.Basic Pandas Reading Method <a id=\"1\"> </a> \n ---\n [**Go to top**](#00)\n \n ![](https://python-graph-gallery.com/wp-content/uploads/Pandas_Cheat_Datacamp.png)\n ![](https://ugoproto.github.io/ugo_py_doc/img/scipy_cs/Pandas_Cheat_Sheeta.png)\n ![](https://cdn-images-1.medium.com/max/2000/1*YhTbz8b8Svi22wNVvqzneg.jpeg) "},{"metadata":{"trusted":true,"_uuid":"2013cff9b3d848e1923926032534ef7d305d9be4"},"cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt\nimport numpy as np\n%matplotlib inline\nimport warnings\nwarnings.simplefilter(action='ignore', category=FutureWarning)\nwarnings.simplefilter(action='ignore', category=RuntimeWarning)\nwarnings.simplefilter(action='ignore', category=UserWarning)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ffa1934d0c985b3cbf0defb72b3da8cc36e75a10"},"cell_type":"code","source":"df = pd.read_csv(\"../input/datasetsdifferent-format/IMDB.csv\", encoding=\"ISO-8859-1\")\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"874f13ff80642477b5c340131cef545b32c58246"},"cell_type":"markdown","source":"# 2.Advance Pandas Reading Methods <a id=\"2\"></a>\n---\n [**Go to top**](#00)\n \n ![](https://i.stack.imgur.com/qCOaK.png)\n \n* [**Advance Pandas Reading Methods**](#2)\n    * [**Manipulating Column & Index Locations and Names**](#21)\n    * [**Data Parsing options**](#22)\n    * [**Reading data from excel files**](#23)\n    * [**Reading data from some other popular formats**](#24)"},{"metadata":{"_uuid":"ef5847c6c5b2ddc6c9a77e183f861ead8dfe87b9"},"cell_type":"markdown","source":"> ### 2.1 Manipulating Columns & Index Location and Names <a id=\"21\"></a>\n\n### 1. No Header and No Columns\n* There is **no header*** and **no columns** while reading csv file here and used `encoding` because file in `ISO-8859-1` format"},{"metadata":{"trusted":true,"_uuid":"5265eee715b0e068a2349b0cfe5383746e263d2a"},"cell_type":"code","source":"df = pd.read_csv('../input/datasetsdifferent-format/IMDB.csv', encoding = \"ISO-8859-1\", header=None)\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"08294d75e254363a4538f82483e9c6c354e19c53"},"cell_type":"markdown","source":"### 2.Specify a different row as header\n* Read specific **rows as header** which is working as **column name**\n* In the result, row 2 become header of dataframe"},{"metadata":{"trusted":true,"_uuid":"750fcf0945cb137ca4db9c504a16f48532a943d8"},"cell_type":"code","source":"df = pd.read_csv(\"../input/datasetsdifferent-format/IMDB.csv\", encoding = \"ISO-8859-1\", header=2)\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"579d6d098ca78625931c91013b093d5e6b2ccbf1"},"cell_type":"markdown","source":"### 3.Specify a column as index\n* Read specify column as index for dataframe\n* In the result, title column become Index column of dataframe"},{"metadata":{"trusted":true,"_uuid":"eaa4eb0bb9c5a3c63bd996147d1ac4632ef1e47a"},"cell_type":"code","source":"df = pd.read_csv(\"../input/datasetsdifferent-format/IMDB.csv\", encoding = \"ISO-8859-1\", index_col=\"Title\")\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9b059a5914a12924ebb48694c933b5c80e22eaf3"},"cell_type":"markdown","source":"### 4.Choose only a subset of columns to be read\n* Subset specific columns from the dataframe while reading file\n* In the result, subset the ` Title, Genre1, Genre2, Budget` columns"},{"metadata":{"trusted":true,"_uuid":"f4aa41dc2b262bc2a2eebef760bbba9f2791ba0a"},"cell_type":"code","source":"df = pd.read_csv(\"../input/datasetsdifferent-format/IMDB.csv\", encoding = \"ISO-8859-1\", usecols=['Title','Genre1','Genre2','Budget'])\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"33993b9f2fca1e51dd0bea77f946e61b107d6bfc"},"cell_type":"markdown","source":"### 5.Handling Missing and NA data\n\n***Missing Value format :***  NaN: ”, ‘#N/A’, ‘#N/A N/A’, ‘#NA’, ‘-1.#IND’, ‘-1.#QNAN’, ‘-NaN’, ‘-nan’, ‘1.#IND’, ‘1.#QNAN’, ‘N/A’, ‘NA’, ‘NULL’, ‘NaN’, ‘nan’`.\n\n* Handling Missing value while reading data.\n* In the result, dataframe handle the result which contain `nan` kind missing value"},{"metadata":{"trusted":true,"_uuid":"7dbad608ce9b01300bca013668eae61d2a2828aa"},"cell_type":"code","source":"df = pd.read_csv('../input/datasetsdifferent-format/IMDB.csv', encoding = \"ISO-8859-1\", na_values=['nan'])\ndisplay(df.head())\nprint(df.shape)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b65eef0d3d330b1608bcd7afad673711ca53de54"},"cell_type":"markdown","source":"### 6.Choose whether to skip over blank rows or not\n\n* you choose whether to skip over blank rows while reading data\n* In the result, you can see that we have skipped blank rows.\n"},{"metadata":{"trusted":true,"_uuid":"10ab61642b1b00e58ea82fa2535e35864653eba7"},"cell_type":"code","source":"df = pd.read_csv('../input/datasetsdifferent-format/IMDB.csv', encoding = \"ISO-8859-1\", skip_blank_lines=False)\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ee0eda98b03f87df896b6b249eab34457a758741"},"cell_type":"markdown","source":"> ### 2.2 Data Parsing options <a id=\"22\"> </a>\n\n### 1. Skip Rows\n* We can skip the rows by reading the dataset\n* In the result, you can see that row number `1,3,7` are skipped from the dataframe."},{"metadata":{"trusted":true,"_uuid":"0007420c303217d629eb1fb838e5706fa1b1166c"},"cell_type":"code","source":"df = pd.read_csv('../input/datasetsdifferent-format/IMDB.csv', encoding = \"ISO-8859-1\", skiprows = [1,3,7])\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"bd774460f6a6df8f91736f9969be5eca18c67017"},"cell_type":"markdown","source":"### 2.Skip rows from footer or from end of the file\n\n* We can skip the rows from the footer.\n* In the result, "},{"metadata":{"trusted":true,"_uuid":"e8f0a40f628ab1087eb755d14d4d005b50db4697"},"cell_type":"code","source":"df.tail(2)\nprint(\"After Skipping the Rows\")\ndf = pd.read_csv('../input/datasetsdifferent-format/IMDB.csv', encoding = \"ISO-8859-1\", skipfooter=2, engine='python')\ndf.tail(2)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0b64a249ab7243461062d00e86fb97c5ee21b3b6"},"cell_type":"markdown","source":"### 3.Reading only a subset of the file or a certain number of rows\n\n* We are also Reading only a subset of the file or a certain number of rows while reading whole dataset file.\n* In the result, we can see the shape of the data before and after."},{"metadata":{"trusted":true,"_uuid":"39dc72f18ba37cd62d186860d5e7cfbfca0acad2"},"cell_type":"code","source":"print(\"Before Shape:\",df.shape)\nprint(\"After Selecting 100 Rows\")\ndf = pd.read_csv('../input/datasetsdifferent-format/IMDB.csv', encoding = \"ISO-8859-1\", nrows=100)\nprint(\"After Shape:\",df.shape)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"cc48f56dd7a7c3c24e75d303ddd313877f85bd4d"},"cell_type":"markdown","source":"> ### 2.3.Reading data from excel files <a id=\"23\"></a>\n\n### 1.Basic Excel read\n* Basic Excel file reading with default sheet number"},{"metadata":{"trusted":true,"_uuid":"892798063ece437ac8ad6c5c7e29eb5b807452ea"},"cell_type":"code","source":"df = pd.read_excel('../input/datasetsdifferent-format/IMDB.xlsx')\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e3bf37852aee430f60c2b4579421c93a7432a56e"},"cell_type":"markdown","source":"## Advanced read options\n\n`pandas.read_excel(io, sheetname=0, header=0, skiprows=None, skip_footer=0, index_col=None, names=None, parse_cols=None, parse_dates=False, date_parser=None, na_values=None, thousands=None, convert_float=True, has_index_names=None, converters=None, dtype=None, true_values=None, false_values=None, engine=None, squeeze=False, **kwds)`\n\n***Reference:*** [Pandas Doc](http://pandas.pydata.org/pandas-docs/version/0.20/generated/pandas.read_excel.html)\n\n### 2.Which Sheet to read?\n* We can select which sheet which we have to read."},{"metadata":{"trusted":true,"_uuid":"8d233c938e9366f4eba90ae1ec943a123635f497"},"cell_type":"code","source":"df = pd.read_excel('../input/datasetsdifferent-format/IMDB.xlsx', sheet_name=0)\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8ae73a56d8cc6ff09da94d8e306d9301d403acb2"},"cell_type":"markdown","source":"### 3.Reading data from multiple sheets in an excel file\n* Find out the sheet list of the excel file"},{"metadata":{"trusted":true,"_uuid":"0a20ca9bff983a3cf4235747b7c14f4bcc693027"},"cell_type":"code","source":"df_excel = pd.ExcelFile('../input/datasetsdifferent-format/IMDB.xlsx')\ndf_excel.sheet_names","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3f68aa745dc513893d650899da432ca8bb0bf115"},"cell_type":"code","source":"df1 = df_excel.parse('movies')\ndf2 = df_excel.parse('by genre')\ndf1.head()\ndf2.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f91f240361730172a6fc594cb94a13d8daa3e206"},"cell_type":"markdown","source":"### 4.Choose Header or column labels\n\n* we can also select header or columns labels from the `read_excel()`  function"},{"metadata":{"trusted":true,"_uuid":"e5c5560acc7253f9ff7a1cf3953c73517857aa22"},"cell_type":"code","source":"df = pd.read_excel('../input/datasetsdifferent-format/IMDB.xlsx', sheet_name=1, header=3)\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"13fd726b3eff8b3bff121aafb75a41b661ffaf81"},"cell_type":"markdown","source":"### 5.No header\n* We can set `header = None` for not seeing header"},{"metadata":{"trusted":true,"_uuid":"e141cd99952beebdfc5cf54efd844c819983da8b"},"cell_type":"code","source":"df = pd.read_excel('../input/datasetsdifferent-format/IMDB.xlsx', sheet_name=1, header=None)\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"de562cd865358ff0204833a4f8e39fff69f4674e"},"cell_type":"markdown","source":"### 6.Skip Rows at the beginning of the file\n* Skip the rows"},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"5efd37ea4d594331b30954a4c7ce6231ca53430e"},"cell_type":"code","source":"df = pd.read_excel('../input/datasetsdifferent-format/IMDB.xlsx', sheet_name=1, skiprows=7)\ndf.head(10)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"caea4db03dbaf4eb71c6a358bf229a64f75b6855"},"cell_type":"markdown","source":"### 7.Skip rows from the end of the file"},{"metadata":{"trusted":true,"_uuid":"2066ad264aaf7583b824c7283fcd31f0070d9588"},"cell_type":"code","source":"df = pd.read_excel('../input/datasetsdifferent-format/IMDB.xlsx', sheet_name=1, ski_footer=10)\ndf.tail(10)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"dd6dc7c428fd94d9dcae0e284ead219aa5fc9f4f"},"cell_type":"markdown","source":"### 8.Choose Columns\n* we can choose column from the excel file"},{"metadata":{"trusted":true,"_uuid":"c278735e5e7dba13cfd6fe404a0bf1f140183dfd"},"cell_type":"code","source":"df = pd.read_excel('../input/datasetsdifferent-format/IMDB.xlsx', sheet_name= 0, usecols=2)\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"de629441fc6b8e92d4fbfaebc65174af631ddf21"},"cell_type":"markdown","source":"### 9.Column Names"},{"metadata":{"trusted":true,"_uuid":"51f7cc408c97eeec0870ebb60b97d530c0267b64"},"cell_type":"code","source":"df = pd.read_excel('../input/datasetsdifferent-format/IMDB.xlsx', sheet_name=0, usecols = 2, names=['X','Title', 'Rating'], )\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"55e85355e5ff998b77506eacdde582fa7b382d4c"},"cell_type":"markdown","source":"### 10.Set an Index while reading data"},{"metadata":{"trusted":true,"_uuid":"692892ef2ab621063fd916016da68fb7f91b4b0c"},"cell_type":"code","source":"df = pd.read_excel('../input/datasetsdifferent-format/IMDB.xlsx', sheet_name=0, index_col='Title')\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8a1013a08809f0470c29de85fe93fc57c6bd04ec"},"cell_type":"markdown","source":"### 11.Handle missing data while reading"},{"metadata":{"trusted":true,"_uuid":"2cc691a70994aeb7563ace2057bbcca177cc0bc7"},"cell_type":"code","source":"df = pd.read_excel('../input/datasetsdifferent-format/IMDB.xlsx', sheet_name= 0, na_values=['nan']) ## as per missing value\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"45ea1503c41eb295b902791fbeff5fbee68f6512"},"cell_type":"markdown","source":"> ### 2.4.Reading data from some other popular formats <a id=\"24\"></a>\n### 1.Reading JSON data into Pandas"},{"metadata":{"trusted":true,"_uuid":"a2ebcc0cc977623af067c778fbc7c4434b6c4ffa"},"cell_type":"code","source":"movies_json = pd.read_json('../input/datasetsdifferent-format/IMDB.json')\nmovies_json.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8b4c9ad82bfe20ed21eb28183ffd751f102c9495"},"cell_type":"markdown","source":"### 2.Reading HTML data"},{"metadata":{"trusted":true,"_uuid":"364096de4343fd462eebf1041823c4a1e432b35e"},"cell_type":"code","source":"df = pd.read_html('../input/datasetsdifferent-format/IMDB.html')\n# df","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"5fe67038e4aa9ce51904b41cdb99a72f81632b2d"},"cell_type":"markdown","source":"### 3.Read pickle file"},{"metadata":{"trusted":true,"_uuid":"584732d7f0660d5d78b22a0a316c75b4b6cb53cd"},"cell_type":"code","source":"df = pd.read_pickle('../input/datasetsdifferent-format/IMDB.p')\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7348b59b3fd1fada9e0b4b17f0a42ec5ad2dc634"},"cell_type":"markdown","source":"### 4.Read SQL file"},{"metadata":{"trusted":true,"_uuid":"b40768bf9b94c37e8faa41b64c2103bf95989b0d"},"cell_type":"code","source":"import sqlite3","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"815927487c93eea72212c13e5dd6c5c33fae7871"},"cell_type":"code","source":"conn = sqlite3.connect(\"../input/datasetsdifferent-format/IMDB.sqlite\")\ndf = pd.read_sql_query(\"SELECT * FROM IMDB;\", conn)\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"fbe7564a083556b731fd8b0ccc972e66d7ff2315"},"cell_type":"markdown","source":"### 5.Read data from clipboard"},{"metadata":{"trusted":true,"_uuid":"0f56f90b33ffaee4a950199d8286f12a88fb4388"},"cell_type":"code","source":"# df = pd.read_clipboard()\n# # df.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"17eb6411108be9e6c96fe4983826509020d66d57"},"cell_type":"markdown","source":"# 3.Apply multiple filter criteria to a pandas DataFrame<a id=\"3\"></a>\n---\n [**Go to top**](#00)\n \n ![](https://docs.microsoft.com/en-us/dynamics365/customer-engagement/social-engagement/media/data-set-concept-social-engagement.png)\n ### In this section, you will learn\n1. Filter using `&` **AND Operator.**\n1. Filter using `|`  **OR Operator.**\n1. Filtering using *`isin`* **method**\n1. Using ***`isin` method*** with multiple conditions\n \n###  1.Read in the dataset"},{"metadata":{"trusted":true,"_uuid":"c5a1d7b807ffd9a07f768827d83025f14db09d24"},"cell_type":"code","source":"data_zillow = pd.read_table('../input/datasetsdifferent-format/data-zillow.csv', sep=',')\ndata_zillow.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"413795c3fa6a2947e100b90001d2f2ac9178c651"},"cell_type":"markdown","source":"### 2. FIlter Based on Multiple Condition"},{"metadata":{"trusted":true,"_uuid":"1b41aa04401ea26bca1613144a5642ca47274793"},"cell_type":"code","source":"data_zillow[(data_zillow['Zhvi'] > 1000000) & (data_zillow['State'] == 'NY')].head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"955854dc68d314209ad4d77486b8d765521fcb2d"},"cell_type":"code","source":"data_zillow[((data_zillow['State'] == 'CA') | (data_zillow['State'] == 'NY'))].head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"56e2d0101d7f1561c1d6ce99aa1b5b5a24452ab2"},"cell_type":"code","source":"zillow_filter = data_zillow['Metro'].isin(['New York','San Diego'])\ndata_zillow[zillow_filter].head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"24f583af9ea5055c9e8b1d0a20e895419fe3e94a"},"cell_type":"code","source":"zillow_filter1 = data_zillow.isin({'State': ['CA'], 'Metro': ['San Francisco']})\ndata_zillow[zillow_filter1].head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ea28db8b7db9d86d9f80423ed46e840f397d166a"},"cell_type":"markdown","source":"# 4.Changing the datatype of a Pandas Series <a id=\"4\"></a>\n---\n[**Go to Top**](#00)\n\n![](https://cdn-images-1.medium.com/max/1600/1*oErPCXv1PFcuuizXqGEEbw.png)\n### In this section you will learn\n1. Changes Data int to float\n2. Changing datatype while reading data\n3. Converting string to datetime\n\n\n#### 1.Read Dataset"},{"metadata":{"trusted":true,"_uuid":"93a0db48fed56c47d60a2e9057fee6d7aa6211c9"},"cell_type":"code","source":"data_zillow = pd.read_table('../input/datasetsdifferent-format/data-zillow.csv', sep=',')\ndata_zillow.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3087f29893ef44d873e10f5fa5c687170daf91e7"},"cell_type":"markdown","source":"#### 2. Changes Data int to float"},{"metadata":{"trusted":true,"_uuid":"dc65e1403cd5ef423af7e4246a2b6f00dc9f2430"},"cell_type":"code","source":"data_zillow.dtypes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fbf40f0362b01c6dd98e496b2fef071ca9655166"},"cell_type":"code","source":"data_zillow['Zhvi'] = data_zillow.Zhvi.astype(float)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e24a30d90c5426575338eb43adc16b0e55f1cb1e"},"cell_type":"code","source":"data_zillow.dtypes","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"37d880da6ffad779f60fd944081adc25fc86e78f"},"cell_type":"markdown","source":"### 3.Changing datatype while reading data\n* By using `dtype` parameter in reading function we can change data types of any column as per below example"},{"metadata":{"trusted":true,"_uuid":"b1fcbfadb1b5d29e9e7eb78c444670c89d8dfb04"},"cell_type":"code","source":"data_zillow1 = pd.read_csv('../input/datasetsdifferent-format/data-zillow.csv', sep=',', dtype={'Zhvi':float})\ndata_zillow1.dtypes","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"20c7cc9721e0dcd1f892966016ee83e9ff281e6d"},"cell_type":"markdown","source":"### 4.Converting string to datetime\n* we can also change *`date`* data type by using `pd.to_datetime()`"},{"metadata":{"trusted":true,"_uuid":"e0627d753f78ab8b0c0863de30984639b9b79ca3"},"cell_type":"code","source":"pd.to_datetime(data_zillow1.Date,infer_datetime_format=True).head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8a447e68c5abd9d1b2158fa2aa651b9606894d2d"},"cell_type":"markdown","source":"# 5.Filter rows of a pandas DataFrame by column value <a id=\"5\"></a>\n---\n [**Go to top**](#00)\n\n![](http://104.236.88.249/wp-content/uploads/2016/10/Pandas-selections-and-indexing.png)\n\n### In this section, you will learn\n1. Filtering Method by using `filter()`\n2. Filtering Method by Regular expression in `filter()` function\n3. Filter data using boolean indexing\n4. An alternative way to filter\n\n#### 1. Read Dataset"},{"metadata":{"trusted":true,"_uuid":"c6d6c02657f0d6ae2603c6b18fb313176b831cc4"},"cell_type":"code","source":"data = pd.read_table('../input/datasetsdifferent-format/data-zillow.csv', sep=',')\ndata.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f74b48baf55db3db6dfdbb2cecb212b53cf86810"},"cell_type":"markdown","source":"#### 2.Filter columns by Different Ways\n* Filtering Method by using `filter()`\n* Filtering Method by Regular expression in `filter()` function\n* Filter data using boolean indexing\n* An alternative way to filter"},{"metadata":{"trusted":true,"_uuid":"8fa14aea076261376b133eb4069bae03b365b24c"},"cell_type":"code","source":"filtered_data = data.filter(items=['State', 'Metro'])\nfiltered_data.head(6)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2e093e9211d2739bdcb5300acda873bb52930c6f"},"cell_type":"markdown","source":"#### 3.Filter columns by regular expression using filter()"},{"metadata":{"trusted":true,"_uuid":"8c35fce48a46ee20f3f226e5f6120772b7e9d369"},"cell_type":"code","source":"filtered_data = data.filter(regex='Region', axis=1)\nfiltered_data.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9b46d2af12421b848afdfe9bf8f9455ff7ae1cc2"},"cell_type":"markdown","source":"#### 4.Filter data using boolean indexing"},{"metadata":{"trusted":true,"_uuid":"141892021173267ed9cd2edbe7c0c7231a4f37bd"},"cell_type":"code","source":"price_filter_series = data['Zhvi'] > 500000\nprice_filter_series.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7784a5c079b2340c38bcbc77d71d306bdd804e45"},"cell_type":"code","source":"data[price_filter_series].head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e678f9ce3a5e3ae498c4144bb5ce20bb06bcc2f1"},"cell_type":"markdown","source":"#### 5.An alternative way to filter"},{"metadata":{"trusted":true,"_uuid":"d6a04e810b53ef415c45b0fc154cfe7d0173bc72"},"cell_type":"code","source":"data[data.Zhvi >= 1000000].head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"62bdcf35796634d18a388848e0bd9dd571470e84"},"cell_type":"markdown","source":"# 6.Selecting multiple rows and columns from a pandas DataFrame <a id=\"6\"> </a>\n---\n [**Go to top**](#00)\n \n \n### In this Section you can learn:\n\n1. Select single row, single column\n1. Select single row, multiple columns\n1. Select single row, all columns\n1. Select multiple rows, single column\n1. Select multiple rows and multiple contiguous columns\n1. Select multiple rows and multiple non-contiguous columns\n1. Select multiple rows and all columns\n1. Select non-contiguous rows\n1. Selecting rows based on a specific column's value\n1. Selecting all rows for a specific column based on a value of another column\n\n### 1.Read dataset"},{"metadata":{"trusted":true,"_uuid":"62a4ff323d8368f403fdaad9c7adf68f9a5f6360"},"cell_type":"code","source":"data_zillow = pd.read_table('../input/datasetsdifferent-format/data-zillow.csv', sep=',')\ndata_zillow.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0171cdcb1d3eb13990638e38aacaa1972fc1de51"},"cell_type":"markdown","source":"### 2.Select single row, single column"},{"metadata":{"trusted":true,"_uuid":"2c0a3e9aaba52d4f07dce5bb28945a8e14ece0d0"},"cell_type":"code","source":"data_zillow.loc[7, 'Metro']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"beb3d57d8ba0cffbc47aaa6a86cf2c51c0f881b9"},"cell_type":"code","source":"data_zillow.iloc[7,4]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"593675eedf7c58de6effa7f28be47b92ed9b55f2"},"cell_type":"markdown","source":"### 3.Select single row, multiple columns"},{"metadata":{"trusted":true,"_uuid":"59da74f120a44865d5cc6bc66e034b4c02d60b2c"},"cell_type":"code","source":"data_zillow.loc[7, ['Metro', 'County']]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"74ce54f0cee66073d0de3b225258160123a9552d"},"cell_type":"code","source":"data_zillow.iloc[7, [4,5]]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b77ccba774d2d0197b35164dd332377392ed824a"},"cell_type":"markdown","source":"### 4.Select single row, all columns"},{"metadata":{"trusted":true,"_uuid":"76ebe4edd971cb606987152e428a19afd4035172"},"cell_type":"code","source":"data_zillow.loc[11, :]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"28850cf25cf9a821d30336841253d722a6a4db1e"},"cell_type":"markdown","source":"### 5.Select multiple rows, single column"},{"metadata":{"trusted":true,"_uuid":"1e2c1c80b775f7259ca073d8d011bf3d542fa4db"},"cell_type":"code","source":"data_zillow.loc[101:105, 'Metro']","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d314c190a15e3cf9353fd324602b50c46fa9b66c"},"cell_type":"markdown","source":"### 6.Select multiple rows and multiple contiguous columns\n\n* **In `loc`**  we pass the column label to fetch data.\n* **In `iloc`**  we pass the number to fetch data."},{"metadata":{"trusted":true,"_uuid":"a705c5d817ed2153211c4a1125eeba7a0b80e338"},"cell_type":"code","source":"data_zillow.loc[201:204, \"State\":\"County\"]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3d1d15375f4ebc1f3fd899ebe55eb1604174d202"},"cell_type":"code","source":"data_zillow.iloc[201:205, 3:6]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a8966749d269438d65df99633fed40840507a819"},"cell_type":"markdown","source":"### 7.Select multiple rows and multiple non-contiguous columns"},{"metadata":{"trusted":true,"_uuid":"dfb6a748589c4cde8ad1820cbbdfc29f56ee0664"},"cell_type":"code","source":"data_zillow.loc[201:205, ['RegionName', 'State']]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"84529df0072f54388a4018afe81f7213283684c2"},"cell_type":"markdown","source":"### 8.Select multiple rows and all columns"},{"metadata":{"trusted":true,"_uuid":"6824c8aa1473fa11c7082474566ed223449fa0aa"},"cell_type":"code","source":"data_zillow.loc[201:205, :]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7144be8f55095681af806b286a9f2499d6ae9d4e"},"cell_type":"markdown","source":"### 9.Select non-contiguous rows"},{"metadata":{"trusted":true,"_uuid":"c5685535872165c7e49c70c2e29320cf580ef6b7"},"cell_type":"code","source":"data_zillow.loc[[0,5,10], :]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"585ffc5545c57c1059aabee355875fd098ae03b5"},"cell_type":"markdown","source":"### 10.Selecting rows based on a specific column's value"},{"metadata":{"trusted":true,"_uuid":"2ed5994091954d738fc8579cd49562ac3705d204"},"cell_type":"code","source":"data_zillow.loc[data_zillow.County==\"Queens\"]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b50e6cfebafdd7410b9c1ba4fa75d89cad72db68"},"cell_type":"markdown","source":"### 11.Selecting all rows for a specific column based on a value of another column"},{"metadata":{"trusted":true,"_uuid":"18b0420a6eaf28da301625a37fe871c1fb89fcd8"},"cell_type":"code","source":"data_zillow.loc[data_zillow.Metro==\"New York\", \"County\"].head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2f7dc2e8ca7d307e7bc13f3052c1bac5f530f436"},"cell_type":"markdown","source":"# 7.Sorting a pandas DataFrame or a Series <a id=\"7\"></a>\n---\n[**Go to top**](#00)\n\n![](https://www.notquitesusie.com/wp-content/uploads/2012/10/farmers-market-coloring-sorting-set.jpg)\n\n### In this section you can learn:\n\n1. Simple sort\n1. Changing the sort order\n1. Sort by more than one column\n1. Sort by multiple columns and mixed ascending order\n1. Sort a Series\n\n### 1.Read dataset"},{"metadata":{"trusted":true,"_uuid":"a589700f06ff02dddae557afba57609b23ef8e82"},"cell_type":"code","source":"data_zillow = pd.read_table('../input/datasetsdifferent-format/data-zillow.csv', sep=',')\ndata_zillow.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"004616e4f857e4d1cd5c7a31833fa9954dccfe8e"},"cell_type":"markdown","source":"### 2.Simple sort\n* Sort the value by using column name"},{"metadata":{"trusted":true,"_uuid":"6a547e6f11ca4f14707c366e30bb81d062f96e80"},"cell_type":"code","source":"data_zillow.sort_values('Metro').head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7b7b9b398f259c8b9361709a7405f1bf70424fcf"},"cell_type":"markdown","source":"### 3.Changing the sort order\n* Sorting the value basis on the descending order"},{"metadata":{"trusted":true,"_uuid":"74060e2001dbfb9eb8f63d4818d57f8c8c4a9951"},"cell_type":"code","source":"sorted = data_zillow.sort_values('Metro', ascending=False)\nsorted.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a28385554e6e61a0e5909b0c975f2075a2958695"},"cell_type":"markdown","source":"### 4.Sort by more than one column"},{"metadata":{"trusted":true,"_uuid":"fb15d1ebc7d0406ae6a6201a2681804768059882"},"cell_type":"code","source":"sorted = data_zillow.sort_values(by=['Metro','County'])\nsorted.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"18dd9b184d34bf47a3129c3caab1852ec74c32eb"},"cell_type":"markdown","source":"### 5.Sort by multiple columns and mixed ascending order"},{"metadata":{"trusted":true,"_uuid":"871b4d89addc1741d79ab58dda35798fa369a2a4"},"cell_type":"code","source":"sorted = data_zillow.sort_values(by=['Metro','County', 'Zhvi'], \n                            ascending=[True, True, False])\nsorted.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9acb5996c97db4fe17dc08300bdf824116f6033e"},"cell_type":"markdown","source":"### 6.Sort a Series\n\n* 1.Let's create a Series object"},{"metadata":{"trusted":true,"_uuid":"a8e2f43674423ca056544339ced4d67c55281867"},"cell_type":"code","source":"regions = data_zillow.RegionID\ntype(regions)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"12794c9bdfc00907d20a5ff05a983597845dde5b"},"cell_type":"markdown","source":"**Let's sort the series¶**\n* **1.Original Series**"},{"metadata":{"trusted":true,"_uuid":"e9877f445f344622bbeb484e3705ce2a955788a0"},"cell_type":"code","source":"regions.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"87a2abb1524e7585f6b3e88e1eea6f8e7b10ef0c"},"cell_type":"markdown","source":"* **2.Sorted**"},{"metadata":{"trusted":true,"_uuid":"0b80c1a3bf6b8b32f32c90aff466af290fa1f3e1"},"cell_type":"code","source":"regions.sort_values().head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"84158ba6779216a8939efaba339167dd92a2544d"},"cell_type":"markdown","source":"# 8.Using pandas Series data structure to select a subset of the data <a id=\"8\"></a>\n---\n[**Go to top**](#00)\n\n![](https://image.slidesharecdn.com/talk-120111102959-phpapp01/95/a-look-inside-pandas-design-and-development-23-728.jpg)\n\n### In this Section, you will learn below topics\n\n1. Select data\n    * Select a Series with bracket notation\n2. DataFrame vs Series\n    * Multi Column Selection - Series or DataFrame\n    * Select using dot notation\n3. Creating a new series by selection"},{"metadata":{"_uuid":"946e7c43d811837fae81b0668a6986dd26dd7550"},"cell_type":"markdown","source":"### 1.Read Dataset"},{"metadata":{"trusted":true,"_uuid":"323a868a7b32c613cc6a340833972dbc81e69435"},"cell_type":"code","source":"data = pd.read_table('../input/datasetsdifferent-format/data-zillow.csv', sep=',')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"75823353510522d724e4f5ab61e3831fd26f6f9e"},"cell_type":"code","source":"data.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"23ed758912e499e705c7fce070c3545dc9af1ca4"},"cell_type":"markdown","source":"### 2.Select data\n* **Select a Series with bracket notation**"},{"metadata":{"trusted":true,"_uuid":"3fca5ee8f633d53b0a7b83cdb34b3dd9e552db1d"},"cell_type":"code","source":"regions = data['RegionName']\ntype(regions)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bba401187ba6815a159538a3ead2afe03a0eb7e0"},"cell_type":"code","source":"regions.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"5336e7deb3f385de384b71d962f98487fe328ad7"},"cell_type":"markdown","source":"### 3.DataFrame vs Series\n* **Multi Column Selection - Series or DataFrame**"},{"metadata":{"trusted":true,"_uuid":"afb08b96760753602cd00c7cd92efe043f7ba93a"},"cell_type":"code","source":"region_n_state = data[['RegionName', 'State']]\nregion_n_state.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7f7137cc9766d9f714e3d0cb5aee3d7c5a5ce5a7"},"cell_type":"code","source":"type(region_n_state)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3b280401fb54a2caaaa28595cb789afea4ec880a"},"cell_type":"markdown","source":"* **Select using dot notation**"},{"metadata":{"trusted":true,"_uuid":"2c7b9b1f1151d8bf28e0c3d90901f067b2c9662d"},"cell_type":"code","source":"data.State.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b60b50c6f145871b82b983ecf22dfc05e438d6a1"},"cell_type":"markdown","source":"### 4.Creating a new series by selection"},{"metadata":{"trusted":true,"_uuid":"d19d04e9c13ba9d3e091cb13bc22d4bdd4334a28"},"cell_type":"code","source":"data['Address'] = data.County + ', ' + data.Metro + ', ' + data.State","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"82b3975782a8bf36a205efd8df590ef501d7c5b3"},"cell_type":"code","source":"data.Address.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"56b7705c00a973b0051b6336101a506c7c8347f6"},"cell_type":"markdown","source":"# 9.Using string methods in pandas <a id=\"9\"></a>\n---\n[**Go To Top**](#00)\n\n### In this section, you will learn\n1. Check for a substring\n2. Make values of a series or column uppercase\n3. Make values lowercase\n4. Get the length of each value in a column\n5. Remove all whitespace from the beginning\n6. Replace parts of a column's values"},{"metadata":{"_uuid":"b1e2434a65c2f9ff8143c8b78e2e67970a7e7fb8"},"cell_type":"markdown","source":"### 1. Read dataset"},{"metadata":{"trusted":true,"_uuid":"76386e29ed05257ce80d9222717cebdafb366a1b"},"cell_type":"code","source":"data = pd.read_table('../input/datasetsdifferent-format/data-zillow.csv', sep=',')\ndata.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"42bf238215d5ff64632d733160c71d478de6ec41"},"cell_type":"markdown","source":"### 2.Check for a substring"},{"metadata":{"trusted":true,"_uuid":"ade0cd23b629fe45650d933eabdec1107116b81d"},"cell_type":"code","source":"data.RegionName.str.contains('New').head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"013788b12a95c8342f46b981b8723aba96adc5d0"},"cell_type":"markdown","source":"### 3.Make values of a series or column uppercase"},{"metadata":{"trusted":true,"_uuid":"329f44fdaf6c41a2a3cf26c6f2bb74b2899925df"},"cell_type":"code","source":"data.RegionName.str.upper().head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"45d353dfef0585bf8214ff34682d0254a2ad09b7"},"cell_type":"markdown","source":"### 4.Make values lowercase\n"},{"metadata":{"trusted":true,"_uuid":"83d33939208be8f3b4475c19c64731e7fb22309a"},"cell_type":"code","source":"data.RegionName.str.lower().head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"78117d56357462279874977341b3345241c52b52"},"cell_type":"markdown","source":"### 5.Get the length of each value in a column"},{"metadata":{"trusted":true,"_uuid":"decbb956f0df489935f3c5569bd9320f09458bc2"},"cell_type":"code","source":"data.County.str.len().head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8fc1665b41988b6f642373311e749ecd3b4cdf5e"},"cell_type":"markdown","source":"### 6.Remove all whitespace from the beginning"},{"metadata":{"trusted":true,"_uuid":"292284730d084ff3b9363afe24e9fc9d9b34e2b3"},"cell_type":"code","source":"data.RegionName.str.lstrip().head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"95d22b4ca2591826ade6be510375391515e4b471"},"cell_type":"markdown","source":"### 7.Replace parts of a column's values"},{"metadata":{"trusted":true,"_uuid":"0c38dbad17108078192d3e10c62b28619fdc6278"},"cell_type":"code","source":"data.RegionName.str.replace(' ', '').head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a913de2eb8531a68ab989277f963d7ddc40d54c1"},"cell_type":"markdown","source":"# 10.Using the axis parameter in pandas<a id=\"10\"></a>\n---\n[**Go To TOP**](#00)\n\n![](https://www.dataquest.io/blog/content/images/2017/12/axis_diagram.jpg)\n\n### In this section, you can learn\n\n1. Usage of axis parameter\n2. axis usage examples\n    * axis = 0\n    * axis = 1\n    * use labels instead of 0 and 1"},{"metadata":{"_uuid":"5b848b4dfd71b4e71c324be89258bbcba44da8ce"},"cell_type":"markdown","source":"### 1.Read Dataset"},{"metadata":{"trusted":true,"_uuid":"7acf3f24c653cc4c05515dd9b69852d5ae32ef73"},"cell_type":"code","source":"data = pd.read_table('../input/datasetsdifferent-format/data-zillow.csv', sep=',')\ndata.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"650fbe79df832ab3f9b929cc8dbb3009f956e5ea"},"cell_type":"markdown","source":"### 2.Usage of axis parameter"},{"metadata":{"trusted":true,"_uuid":"8fdabcdc1b4bbb1316406a5249c3276d83b51c32"},"cell_type":"code","source":"data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9f6516b7ee88954c7e01b83988059277ac82a2db"},"cell_type":"code","source":"data.axes","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b5748ea09979ed1f104c0e1510f7ec7a8c378af5"},"cell_type":"markdown","source":"### 1.**axis = 0**"},{"metadata":{"trusted":true,"_uuid":"0362bfd8a85f9f94b37408f1453390e8a8f0b401"},"cell_type":"code","source":"data.mean(axis=0)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3cb2c4cf0b13166cdc2485e899c267f9db731a13"},"cell_type":"markdown","source":"### 2.axis = 1"},{"metadata":{"trusted":true,"_uuid":"19b7f672d56b307f39f1f851d50e25de4a166f86"},"cell_type":"code","source":"data.mean(axis=1).head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"90d27f42336c1349fcc8b32e7c852c7694c9c40f"},"cell_type":"markdown","source":"### 3.use labels instead of 0 and 1"},{"metadata":{"trusted":true,"_uuid":"42aecaf3aba29bee84c38f6b51adfbbc2a8ab2e3"},"cell_type":"code","source":"data.mean(axis='rows')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d6f0b08af0b3ef63d5904e999210550d81e0a86f"},"cell_type":"code","source":"data.mean(axis='columns').head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"83420020fc839458af19316d5da310e082ebe4ed"},"cell_type":"code","source":"data.drop(0, axis=0).head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dd51d2e3bbba27b39138b3c8a4d5a20626ebd6cd"},"cell_type":"code","source":"data.drop('Date', axis=1).head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2c96a6ecb8507f9d61fa4e4e3013395ddcdb7605"},"cell_type":"code","source":"data.drop('Date', axis=1).head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b780dad1ddbb809bb4970064e285b7294c4f30f6"},"cell_type":"markdown","source":"# 11.Applying a function to a pandas Series or DataFrame<a id=\"11\"></a>\n---\n[**Go To TOP**](#00)\n\n![](https://i.stack.imgur.com/AqYhv.png)\n\n### In this section, you can learn\n\n1. Apply functions using apply()\n2. Apply functions using applymap()\n3. Applying our own functions"},{"metadata":{"_uuid":"62729c46bdde975e817d8cd5f559eaa25a0d0069"},"cell_type":"markdown","source":"### 1.Read dataset"},{"metadata":{"trusted":true,"_uuid":"f084a0bb05334f3d87d25550691da33203670fdf"},"cell_type":"code","source":"data = pd.read_csv('../input/datasetsdifferent-format/data-titanic.csv')\ndata.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"74d7fa3380758da28468e34abb395a24c266a1ae"},"cell_type":"markdown","source":"### 2.Apply functions using apply()"},{"metadata":{"trusted":true,"_uuid":"187ec6b6f4dbf781aebd24ba413ecb8eeb36db6e"},"cell_type":"code","source":"func_lower = lambda x: x.lower()\ndata.Name.apply(func_lower).head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8bfb7e56dcf1cfdd18b67f3966a2064837e031fb"},"cell_type":"markdown","source":"### 3.Apply functions using applymap()"},{"metadata":{"trusted":true,"_uuid":"3a2d5a252cefc2354e4b0bc591b20e5625d5c104"},"cell_type":"code","source":"data[['Age', 'Pclass']].applymap(np.square).head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c327a4138c18d2efa5aeff0c4ab5483a34d50c60"},"cell_type":"markdown","source":"### 3.Applying our own functions"},{"metadata":{"trusted":true,"_uuid":"a2a3dca203741e47cc3f38627d5721ba269c0350"},"cell_type":"code","source":"def my_func(i):\n    return i + 20","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3ade8dff119165079e8b4ac524de87d49955941b"},"cell_type":"code","source":"data[['Age', 'Pclass']].applymap(my_func).head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b219146024ecc80dac4a12aeb0a307647a117409"},"cell_type":"markdown","source":"# 12.Handling SettingWithCopyWarning<a id=\"12\"></a>\n---\n[**Go To TOP**](#00)\n\n![](https://www.dataquest.io/blog/content/images/view-vs-copy.png)\n\n### In this section, you can learn\n\n1. A SettingWithCopyWarning scenario\n2. Handling the SettingWithCopyWarning"},{"metadata":{"_uuid":"044a8a5f763fd288b8f66159a86eb6fd56d83c2d"},"cell_type":"markdown","source":"### 1.A SettingWithCopyWarning scenario"},{"metadata":{"trusted":true,"_uuid":"8bec8adf2f445f714e4a7381cec0cbfa9316fbee"},"cell_type":"code","source":"data[data.Age.isnull()].Age = data.Age.mean()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"939cfa644669a17da37532518abc0db59c59cbb3"},"cell_type":"markdown","source":"### 2.Handling the SettingWithCopyWarning"},{"metadata":{"trusted":true,"_uuid":"50faa074dd2c4c1431ad4a32ca119a537795b2a0"},"cell_type":"code","source":"data[data.Age.isnull()].Age.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"233ad0d276a2b9ed32462011ef12881a80213ee5"},"cell_type":"code","source":"data.loc[data.Age.isnull(), 'Age'] = data.Age.mean","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7978b6f3b9a9819bfa7c91e42ac2f3f7507b0a19"},"cell_type":"code","source":"data[data.Age.isnull()]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"19f59d3c81aa929297708dbc3bb2ec98e3417d2d"},"cell_type":"markdown","source":"# 13.Handling missing values in pandas<a id=\"13\"></a>\n---\n[**Go To TOP**](#00)\n\n![](https://cdn-images-1.medium.com/max/1600/1*_RA3mCS30Pr0vUxbp25Yxw.png)\n\n### In this section, you can learn\n\n1. Missing Records \n    1. Find out total records in the dataset\n    1. Number of valid records per column\n2. Dropping missing records\n    1. Drop all records that have one or more missing values\n    1. Drop only those rows that have all records missing\n3. Fill in missing data\n    1. Fill in missing data with zeros\n    1. Fill in missing data with a mean of the values from other rows"},{"metadata":{"trusted":true,"_uuid":"ea32197e3c192da841ce31923793e9b3052a62e4"},"cell_type":"code","source":"data = pd.read_csv(\"../input/datasetsdifferent-format/data-titanic.csv\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c7ca85d63e5972f24a4b87b065fa4bafc178d225"},"cell_type":"markdown","source":"### 1. Missing Records \n1. **Find out total records in the dataset**"},{"metadata":{"trusted":true,"_uuid":"3e6271ae4ff42998a2eedb87ee7f02f7def78089"},"cell_type":"code","source":"data.shape","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3cd684c000fdb34a0ad644a3492d07c0548b0247"},"cell_type":"markdown","source":"2. **Number of valid records per column**"},{"metadata":{"trusted":true,"_uuid":"180e92074bcd20e0f33826ba1ba95c8362a94962"},"cell_type":"code","source":"data.count()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"05885e972eb1aaa034504a193721425b96cb248c"},"cell_type":"markdown","source":"### 2. Dropping missing records\n\n1. **Drop all records that have one or more missing values**\n"},{"metadata":{"trusted":true,"_uuid":"f7de03f21884b47dca8d7f938756dea84baab6e2"},"cell_type":"code","source":"data_missing_dropped = data.dropna()\ndata_missing_dropped.shape","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"037775db411f832eadc6f24e8eb41af794bb0c42"},"cell_type":"markdown","source":"2. **Drop only those rows that have all records missing**"},{"metadata":{"trusted":true,"_uuid":"89253caae0e10ca237cccb9233aaae886828f3b9"},"cell_type":"code","source":"data_all_missing_dropped = data.dropna(how=\"all\")\ndata_all_missing_dropped.shape","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3c30936e71f13c7c575ffef77d9f0a0d6c5c1c1e"},"cell_type":"markdown","source":"### 3. Fill in missing data\n    \n1. **Fill in missing data with zeros**"},{"metadata":{"trusted":true,"_uuid":"7b086ead1e2c6056ff2ab2c89f7d650adb273577"},"cell_type":"code","source":"data_filled_zeros =  data.fillna(0)\ndata_filled_zeros.count()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"72acb9bbbc7b6e0b4d4da444c233eb94a0a74eac"},"cell_type":"markdown","source":"2. **Fill in missing data with a mean of the values from other rows**"},{"metadata":{"trusted":true,"_uuid":"9707b243646e60627633dd97799c553ca8f24d48"},"cell_type":"code","source":"data_filled_in_mean = data.copy()\ndata_filled_in_mean.Age.fillna(data.Age.mean(), inplace=True)\ndata_filled_in_mean.count()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"240939ed7a03b5e831dac34a8d1f2e7d48face2c"},"cell_type":"markdown","source":"# 14.Indexing in pandas dataframes<a id=\"14\"></a>\n---\n[**Go To TOP**](#00)\n\n![](https://bookdata.readthedocs.io/en/latest/_images/base_01_pandas_5_0.png)\n\n### In this section, you can learn\n\n1. Default Index\n2. Set an Index post reading of data\n3. Set an Index while reading data\n4. Selection using Index\n5. Reset Index"},{"metadata":{"trusted":true,"_uuid":"c91dcf2965d02c0b8047688974b7385e88288b11"},"cell_type":"code","source":"data = pd.read_csv('../input/datasetsdifferent-format/data-titanic.csv')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d94121fb0d915104cc60ff6c9bf37306991723e0"},"cell_type":"markdown","source":"### 1.Default Index"},{"metadata":{"trusted":true,"_uuid":"bd4fdaad8ff83b3eced90fb79504d52195cdbac4"},"cell_type":"code","source":"data.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6ae91401ff9f46e352172d4fb34d087da10911a7"},"cell_type":"markdown","source":"### 2. Set an Index post reading of data"},{"metadata":{"trusted":true,"_uuid":"49f04023c64b165e23454840fd89e350e56ab264"},"cell_type":"code","source":"data.set_index('Name').head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"94ae9a5a794ef9462fa03e1ceccb4306656d5016"},"cell_type":"markdown","source":"### 3. Set an Index while reading data"},{"metadata":{"trusted":true,"_uuid":"7e0fe8838eb66fa2696ff4a59fad80bdc68176c9"},"cell_type":"code","source":"data = pd.read_csv('../input/datasetsdifferent-format/data-titanic.csv', index_col=3)\ndata.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4633edbff177eafc27d28f8b4a8431b5171a2d3a"},"cell_type":"markdown","source":"### 4. Selection using Index"},{"metadata":{"trusted":true,"_uuid":"4d1264022da5ca65d3f84d26eae66bfe04201b1f"},"cell_type":"code","source":"data.loc['Braund, Mr. Owen Harris',:]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0c96097582281be77a84791c2eac5b933c626ab8"},"cell_type":"markdown","source":"### 5. Reset Index"},{"metadata":{"trusted":true,"_uuid":"59f597ce7a1f4f97e0ede149035252d9a54232f4"},"cell_type":"code","source":"data.reset_index(inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5bec37b4ea3aee8149751505cacbe8b246efd4d7"},"cell_type":"code","source":"data.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c877b36e639fb37d0cb33c47a75051ac927070d9"},"cell_type":"markdown","source":"# 15.Merging and concatenating multiple data frames into one<a id=\"15\"></a>\n---\n[**Go To TOP**](#00)\n\n![](https://cdn-images-1.medium.com/max/1600/1*uG1vjoSQj7gMm8craCj2xA.png)\n\n### In this section, you can learn\n\n1. Concatenate Dataset DataFrames\n2. Concatenate using append()\n3. Concatenate on columns\n4. Merging DataFrames\n5. Left outer merge\n6. Right outer merge\n7. Full outer merge"},{"metadata":{"_uuid":"d4778912dcb549a9628d5c9631d31f1e4a9b6cee"},"cell_type":"markdown","source":"### 1. Concatenate Dataset DataFrames\n"},{"metadata":{"trusted":true,"_uuid":"64a4397c872c4da6c87e687af394126cb6db70e6"},"cell_type":"code","source":"dataset1 = pd.DataFrame({'Age': ['32', '26', '29'],\n                         'Sex': ['F', 'M', 'F'],\n                         'State': ['CA', 'NY', 'OH']},\n                         index=['Jane', 'John', 'Cathy'])\n    \ndataset2 = pd.DataFrame({'Age': ['34', '23', '24', '21'],\n                         'Sex': ['M', 'F', 'F', 'F'],\n                         'State': ['AZ', 'OR', 'CA', 'WA']},\n                         index=['Dave', 'Kris', 'Xi', 'Jo'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bf3f6d94018ee8d6957fe59f67e8c0db779ef4d5"},"cell_type":"code","source":"pd.concat([dataset1, dataset2])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ebca9041c04ba97a409b10ca05c7e9d63fb8ea76"},"cell_type":"markdown","source":"### 2. Concatenate using append()"},{"metadata":{"trusted":true,"_uuid":"fec8d4c508f50d2909e6e2722227330c09043383"},"cell_type":"code","source":"dataset1.append(dataset2)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b742b03b80842138fe6b0d94c18f80308fa851cd"},"cell_type":"markdown","source":"### 3. Concatenate on columns\n"},{"metadata":{"trusted":true,"_uuid":"c00a9eefe0597423206ad605b3ab3081a6fe488d"},"cell_type":"code","source":"dataset1 = pd.DataFrame({'Age': ['32', '26', '29'],\n                         'Sex': ['F', 'M', 'F'],\n                         'State': ['CA', 'NY', 'OH']},\n                         index=['Jane', 'John', 'Cathy'])\n\ndataset2 = pd.DataFrame({'City': ['SF', 'NY', 'Columbus'],\n                         'Work Status': ['No', 'Yes', 'Yes']},\n                         index=['Jane', 'John', 'Cathy'])\n\n\npd.concat([dataset1, dataset2], axis=1)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9b00fda156ce029913258a97c359684589e740f8"},"cell_type":"markdown","source":"### 4. Merging DataFrames"},{"metadata":{"trusted":true,"_uuid":"be24ce790b850058039192352401eba19387123b"},"cell_type":"code","source":"dataset1 = pd.DataFrame({'Name': ['Jane', 'John', 'Cathy', 'Sarah'],\n                         'Age': ['32', '26', '29', '23'],\n                         'Sex': ['F', 'M', 'F', 'F'],\n                         'State': ['CA', 'NY', 'OH', 'TX']})\n\ndataset2 = pd.DataFrame({'Name': ['Jane', 'John', 'Cathy', 'Rob'],\n                        'City': ['SF', 'NY', 'Columbus', 'Austin'],\n                         'Work Status': ['No', 'Yes', 'Yes', 'Yes']})\n\npd.merge(dataset1, dataset2, on='Name', how='inner')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"af2d844c0322be00375ad36cd06204874797bf68"},"cell_type":"markdown","source":"### 5. Left outer merge"},{"metadata":{"trusted":true,"_uuid":"65cee7d88741a07d6c91debd85c99f44d8d34ad0"},"cell_type":"code","source":"pd.merge(dataset1, dataset2, on='Name', how='left')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f3eb0a6dc738ecbcb0a632874649f7bc22617f3"},"cell_type":"markdown","source":"### 6. Right outer merge"},{"metadata":{"trusted":true,"_uuid":"6444c3a81886e643817b6ef4b6915449e738c8f0"},"cell_type":"code","source":"pd.merge(dataset1, dataset2, on='Name', how='right')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"934197307fc81866012f68cfba9cb2a79601d062"},"cell_type":"markdown","source":"### 7. Full outer merge"},{"metadata":{"trusted":true,"_uuid":"ed462abb8da36b6db5c3359002634cb5990c96e0"},"cell_type":"code","source":"pd.merge(dataset1, dataset2, on='Name', how='outer')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"80a9f24149a1b275f338fb790a428fa65c40a474"},"cell_type":"markdown","source":"# 16.Modifying a Pandas Dataframe inplace<a id=\"16\"></a>\n---\n[**Go To TOP**](#00)\n\n### In this section, you can learn\n\n1. Modify without inplace\n2. Modify inplace\n3. inplace not required for very method"},{"metadata":{"trusted":true,"_uuid":"2be34010ca01b82e196940e77b6ba7fa891eb58f"},"cell_type":"code","source":"top_movies = pd.read_table('../input/datasetsdifferent-format/data-movies-top-grossing.csv', sep=',')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"31f2f42bdc95d6f0df94d9b2d9f72bd2572c52b4"},"cell_type":"code","source":"top_movies.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"dbe6de69de84aebe54c9e5b90137686af2367698"},"cell_type":"markdown","source":"### 1.Modify without inplace"},{"metadata":{"trusted":true,"_uuid":"aa3a589d3cacd173160e4ab41ed319bb4c0b6646"},"cell_type":"code","source":"top_movies.set_index('Rank').head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"78f2f099e83d56e611f5e72e2d4bea4890f2fa55"},"cell_type":"code","source":"top_movies.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d7f287b63175e029edb46e3752d15d39b0298cb3"},"cell_type":"markdown","source":"### 2.Modify inplace"},{"metadata":{"trusted":true,"_uuid":"9cd60cf21b731e33d02621363dd1696ed781ceac"},"cell_type":"code","source":"top_movies.set_index('Rank', inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cf006af8d2caa042f7a7d77760c7678dbf22ad78"},"cell_type":"code","source":"top_movies.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e7590d95d8c4bd92a3a81f7ddadbf9edb561d0d1"},"cell_type":"markdown","source":"\n### 3.inplace not required for very method"},{"metadata":{"trusted":true,"_uuid":"c6ba1222cd5d7ca86baa4e3738dc1ffbf4d124e7"},"cell_type":"code","source":"top_movies.rename(columns = {'Year': 'Release Year'}).head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6c0231e4bc9d9046c6ff70c0169416784f680a69"},"cell_type":"markdown","source":"# 17.Removing columns from a pandas DataFrame <a id=\"17\"></a>\n---\n[**Go To TOP**](#00)\n\n![](https://i1.wp.com/cmdlinetips.com/wp-content/uploads/2018/04/How_To_Drop_Columns_in_Pandas.jpg)\n\n### In this section, you can learn\n\n1. Remove one column\n2. Remove more than one column\n3. Remove row(s)"},{"metadata":{"_uuid":"0c27babb013188fb3bf535129a6b26fb32af1c07"},"cell_type":"markdown","source":"### 1.Remove one column"},{"metadata":{"trusted":true,"_uuid":"f3b8046794477494b6015b11b86eb6354b4c3e8b"},"cell_type":"code","source":"data = pd.read_csv('../input/datasetsdifferent-format/data-titanic.csv', index_col=3)\ndata.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"62cd9b619226d98730e0b707b171496a48be0faf"},"cell_type":"code","source":"data.drop('Ticket', axis=1, inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"24ed3693a49e899f38a4d3ca1aee2089447eb5d4"},"cell_type":"code","source":"data.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"92f05b2c741cb0af27fc3c4fc4bc384dddb77928"},"cell_type":"markdown","source":"### 2.Remove more than one column"},{"metadata":{"trusted":true,"_uuid":"8a01198341eaf828007f2a221fb53c2642669d3c"},"cell_type":"code","source":"data.drop(['Parch', 'Fare'], axis=1, inplace=True)\ndata.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"16a5e3418d703eacc3f080c35af0789d9f585b3f"},"cell_type":"markdown","source":"### 3.Remove row(s)"},{"metadata":{"trusted":true,"_uuid":"132146b2e3471bb8f186e34e1133ca36c3aa5f73"},"cell_type":"code","source":"data.drop(['Braund, Mr. Owen Harris', 'Heikkinen, Miss. Laina'], inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9ac61480f4ccb1e3ee1d6827cf014cd142bb0007"},"cell_type":"code","source":"data.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"52579047844c6045f74bc88afe0c296f83505733"},"cell_type":"markdown","source":"# 18.Renaming columns in a pandas DataFrame <a id=\"18\"></a>\n---\n[**Go To TOP**](#00)\n\n![](https://image.slidesharecdn.com/datamanagementinpython-170925110242/95/data-management-in-python-19-638.jpg)\n\n### In this section, you can learn\n\n1. Rename columns while reading the data\n2. Rename columns using rename method \n    1. Read in the dataset again \n    2. Rename\n3. Rename all columns"},{"metadata":{"_uuid":"f251685179b1422f9d701bb491fcf9a0f2bdf2ef"},"cell_type":"markdown","source":"### 1.Rename columns while reading the data"},{"metadata":{"trusted":true,"_uuid":"054a502e1f47fc5623057f6336a38b610742adbf"},"cell_type":"code","source":"list_columns = ['Date', 'Region ID', 'Region Name', 'State',\n             'City', 'County', 'Size Rank','Price']\ndata = pd.read_csv('../input/datasetsdifferent-format/data-zillow1.csv', names = list_columns)\ndata.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b3b904d313bafc80cd2d7bddc0c7905316ea63ac"},"cell_type":"markdown","source":"### 2.Rename columns using rename method\n1. **Read in the dataset again**"},{"metadata":{"trusted":true,"_uuid":"18797cfa4e90c95b519a2553a7bfe3c6986143cb"},"cell_type":"code","source":"data = pd.read_csv('../input/datasetsdifferent-format/data-zillow1.csv')\ndata.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"881b4c87f918cc88090a4de2a204c326f76d799b"},"cell_type":"markdown","source":"2. **Rename**"},{"metadata":{"trusted":true,"_uuid":"d6b8eedbe9aea64f15200c1a8ebd986b0e278311"},"cell_type":"code","source":"data.columns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"93bc39e18df54d181af4a41c107e4ab63b71fbd7"},"cell_type":"code","source":"data.rename(columns={'RegionName':'Region', 'Metro':'City'}, inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0594eea707a3f91da75efe6fe8e6c0104bf6304f"},"cell_type":"code","source":"data.columns","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"62fcd30a3893e1c41c5c750e354af6381db43cbb"},"cell_type":"markdown","source":"### 3.Rename all columns"},{"metadata":{"trusted":true,"_uuid":"efdeb10a186cc08baf7b3707a4b7450b645f43fa"},"cell_type":"code","source":"data.columns = ['Date', 'Region ID', 'Region Name', 'State',\n             'City', 'County', 'Size Rank','Price']","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"91b6c91ff2c68acd847c7e17faf58cbfedad66af"},"cell_type":"markdown","source":"# 19.Using groupby method <a id=\"19\"></a>\n---\n[**Go To TOP**](#00)\n\n![](https://i.stack.imgur.com/sgCn1.jpg)\n\n### In this section, you can learn\n\n1. Get Mean price for every State\n2. Split the data into groups\n3. Apply a function on each group and combine the results\n4. Get Descriptive statistics by Groups(States)\n5. Group by data on State and Region\n6. Get the number of records per State\n7. Group by Columns\n8. Iterate over Groups"},{"metadata":{"trusted":true,"_uuid":"680791116e8b32a8b1113ee61f112daa78f5415c"},"cell_type":"code","source":"data = pd.read_csv('../input/datasetsdifferent-format/data-zillow1.csv')\ndata.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"40f98ea89a62a40263102bbedee918c4c0fd4581"},"cell_type":"markdown","source":"### 1.Get Mean price for every State"},{"metadata":{"trusted":true,"_uuid":"3ca4482d813565afbce6cd830f7f709d5844a35e"},"cell_type":"code","source":"grouped_data = data[['State', 'Price']].groupby('State').mean()\ngrouped_data.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e5c503ac2d0aa70f7bf78eca9b129afb9efc02e7"},"cell_type":"markdown","source":"### 2.Split the data into groups"},{"metadata":{"_kg_hide-output":true,"trusted":true,"_uuid":"83d1f18fa0cea7b12c84a8fd388e000e32b32aed"},"cell_type":"code","source":"grouped_data = data[['State', 'Price']].groupby('State')\ngrouped_data.head(2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"74676b8214edccfbf69982ce10f0cee05a5825b9"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"903f0d3e0b8197c413e4e35072e23bb466a411fa"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ea398ecdbcc6d8726cf5ce5df3c20cccb74a9dd0"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7d48fe2602686c67b0dc6d02aa53e2cac9f6e728"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6f6f3242a1b184eb4b2a877e089bc123145a9940"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b783fa494291fd526713bd3d95d5a94d771af44f"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1b106b3a3d6c855ea756290f103dfa90dd0fc3ae"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"73fa5af2a93ddc79695d7ffdf68089fe8334dd89"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a59863aac530be5ef509ad165aed45f192b246a5"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6bebbffce41469bfbf0b22543ffcbcb4c4fb9c8d"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"83c07b76d64d927d4db8545c46210cf8ed5a6682"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ea2226cc70d49d83eb229581a479d4b782176138"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"687d433ad24a6974e86e861e3334936b675fdb74"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7c86ab9ef8a37d37078b113592c40e0856b58402"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8781b6bdac3c8297550211212502a70d7993363f"},"cell_type":"markdown","source":"### 3.Apply a function on each group and combine the results"},{"metadata":{"trusted":true,"_uuid":"f294ed2bc08f427ec94117ee933850b6bd352543"},"cell_type":"code","source":"grouped_data.mean().head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c5ec0b023db358e4f3e64be250186883946a949f"},"cell_type":"markdown","source":"### 4.Get Descriptive statistics by Groups(States)"},{"metadata":{"trusted":true,"_uuid":"5e6a4a59532dcb79e17c7a656d4e7f0c513a7e7e"},"cell_type":"code","source":"grouped_data.describe().head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f9dd60e9e45889cd484d393e86f2998070308557"},"cell_type":"markdown","source":"### 5.Group by data on State and Region"},{"metadata":{"trusted":true,"_uuid":"c593862cc80afa26bb467c28284c4290fab23873"},"cell_type":"code","source":"grouped_data = data[['State',\n                     'RegionName', \n                     'Price']].groupby(['State','RegionName']).mean()\ngrouped_data.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ad75a696ad9ad900080591936f83ca156547dfbb"},"cell_type":"markdown","source":"### 6.Get the number of records per State"},{"metadata":{"trusted":true,"_uuid":"daa4a9c149b05adb143c5f9fdf79383f422a228b"},"cell_type":"code","source":"grouped_data = data.groupby(['State']).size()\ngrouped_data.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3bf8f0e279a32206c1bc6f6d560b799b5761bc8e"},"cell_type":"markdown","source":"### 7.Group by Columns"},{"metadata":{"_kg_hide-output":true,"trusted":true,"_uuid":"22e15f3bd5e04e9d6095f27bf1402a3c9ad258df"},"cell_type":"code","source":"grouped_data = data.groupby(data.dtypes, axis=1)\n# list(grouped_data)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7ec5956bca29419aeec5405eb15aec3ef6f90df8"},"cell_type":"markdown","source":"### 8.Iterate over Groups"},{"metadata":{"trusted":true,"_uuid":"40d1f0fb681cf1361c8313cbcec4f5313030b23b"},"cell_type":"code","source":"# for state, grouped_data in data.groupby('State'):\n#     print(state, '\\n', grouped_data)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"406201335624a1eecb0ba756eb1dfc8ce086e637"},"cell_type":"markdown","source":"# 20.Work with dates and times data <a id=\"20\"></a>\n---\n[**Go To TOP**](#00)\n\n![](https://i.stack.imgur.com/Zfni3.jpg)\n\n### In this section, you can learn\n\n1. let's first convert our date column to datetime\n2. Let's set index to the date column\n3. Filter and select time series Data\n4. Get properties of date-time series data"},{"metadata":{"_uuid":"ad9db367ed7ce00168d0e75ccdc11bffa7da6fd2"},"cell_type":"markdown","source":"### 1.let's first convert our date column to datetime"},{"metadata":{"trusted":true,"_uuid":"3cfa298bfacb5edf42263707a1de63ee04f2d686"},"cell_type":"code","source":"dataset = pd.DataFrame({'DOB': ['1976-06-01', '1980-09-23', '1984-03-30', '1991-12-31', '1994-10-2', '1973-11-11'],\n                        'Sex': ['F', 'M', 'F', 'M', 'M', 'F'],\n                        'State': ['CA', 'NY', 'OH', 'OR', 'TX', 'CA'],\n                        'Name': ['Jane', 'John', 'Cathy', 'Jo', 'Sam', 'Tai']})\ndataset","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"00e01cd3d8ff09aeb8eb5a0b3b88525d69d4fde4"},"cell_type":"code","source":"dataset.dtypes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9a25af8566ccaa80905ba24754b045d7b3a442da"},"cell_type":"code","source":"dataset.DOB = pd.to_datetime(dataset.DOB)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3fa6192a0fa60f4e38ce96854136703534da9a8b"},"cell_type":"code","source":"dataset.dtypes","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"201617691e6b61854160fdfc79341cf8cca39657"},"cell_type":"markdown","source":"### 2.Let's set index to the date column"},{"metadata":{"trusted":true,"_uuid":"d6d8b0ddaca8e65d4474a6c1ecc2c5b480af3c08"},"cell_type":"code","source":"dataset.set_index('DOB', inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"baac800ecdc0abf0f3b92f39b296992e16b5a5ff"},"cell_type":"code","source":"dataset","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"46a8b030bc9c118b188c6528fe8f09764331a258"},"cell_type":"markdown","source":"### 3.Filter and select time series Data"},{"metadata":{"trusted":true,"_uuid":"c6dc828b3306a4533665df839897b912d00f305f"},"cell_type":"code","source":"dataset['1980']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bc4112803f8e0ac0c6ab4af7e917f970d207203f"},"cell_type":"code","source":"dataset['1980':]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bd0dd8ffec076cd9bf0ae07d46d00d0d2ef4691a"},"cell_type":"code","source":"dataset[:'1980']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6db3cf6c78056e896de48a27f9eb42129bfbaeef"},"cell_type":"code","source":"display(dataset['1980':'1984'])\ndataset.reset_index(inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a7ee8354ea6a871c0771f742c21b9a20a735ec7c"},"cell_type":"markdown","source":"### 4.Get properties of date-time series data"},{"metadata":{"trusted":true,"_uuid":"66770eb5818b751ea0e4a5f39f2ff00e849983b8"},"cell_type":"code","source":"dataset.DOB.dt.dayofyear","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4a3c170f7ce93185ca02e63c72d39ab975376709"},"cell_type":"code","source":"dataset.DOB.dt.weekday_name","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7ca27b11d576c37de6a858959d766cb4ca318ce5"},"cell_type":"markdown","source":"# 21.Choosing the colors for the plots <a id=\"211\"></a>\n---\n[**Go To TOP**](#00)\n\n![](https://i.stack.imgur.com/dLUh4.png)\n\n### In this section, you can learn\n\n1. Color Palettes\n2. Look at how these colors look on a plot\n3. Change the color palette\n4. Impact on the plot\n5. seaborn palettes\n6. matplotlib colormaps as color palettes\n7. Let's set the palette to a matplotlib colormap\n8. Impact on the plot\n9. Building custom color palettes\n10. Let's see how the plot has changed"},{"metadata":{"trusted":true,"_uuid":"3f01ce87ab64d504a3f3f2c78c811c19c309783a"},"cell_type":"code","source":"import pandas as pd\nfrom matplotlib import pyplot as plt\n%matplotlib inline\nimport seaborn as sns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1905d0955e8c6ac278ec93bcc21072ade4af5ee1"},"cell_type":"code","source":"df = pd.read_csv('../input/datasetsdifferent-format/data-alcohol.csv')\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6fe753936d55df6c5f52499ae791941ad4a2a5fe"},"cell_type":"markdown","source":"### 1. Color Palettes"},{"metadata":{"trusted":true,"_uuid":"58d4db02168f058ba50c0a33754ad0ccffa6718d"},"cell_type":"code","source":"sns.palplot(sns.color_palette())","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"500d473161642155f8a2a92e4840ac114bfc1afd"},"cell_type":"markdown","source":"### 2.Look at how these colors look on a plot"},{"metadata":{"trusted":true,"_uuid":"39e43519a1a60effe578d8ec7aebdba9c5bc86ac"},"cell_type":"code","source":"plt.figure(figsize = (15,8))\nsns.set()\nsns.boxplot(data=df);","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ea47638fc9f2602c00ebfb1864fc3a52bbffa084"},"cell_type":"markdown","source":"### 3. Change the color palette"},{"metadata":{"trusted":true,"_uuid":"9dd35f4075898f525a08d767cbec18803ed04561"},"cell_type":"code","source":"sns.set_palette(\"bright\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"749f3b7bd11d7fd8a75ad57d3dd39a90cf823ec6"},"cell_type":"markdown","source":"### 4. Impact on the plot"},{"metadata":{"trusted":true,"_uuid":"6b66473d19da8a439373cea4fd662b56fb11e769"},"cell_type":"code","source":"plt.figure(figsize = (15,8))\nsns.boxplot(data=df);","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f543d3b341609a616ef67e751a18e9a68debc04"},"cell_type":"markdown","source":"### 5. seaborn palettes"},{"metadata":{"trusted":true,"_uuid":"7f20a42913a493573c39f1fc5b849c268428ea70"},"cell_type":"code","source":"sns.palplot(sns.color_palette(\"deep\", 7))\nsns.palplot(sns.color_palette(\"muted\", 7))\nsns.palplot(sns.color_palette(\"pastel\", 7))\n\nsns.palplot(sns.color_palette(\"bright\", 7))\nsns.palplot(sns.color_palette(\"dark\", 7))\nsns.palplot(sns.color_palette(\"colorblind\", 7))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"dc1def773ac8561ab75b5c890168ce95f428fd0f"},"cell_type":"markdown","source":"### 6. matplotlib colormaps as color palettes"},{"metadata":{"trusted":true,"_uuid":"7494f9284274a1974f9286864cbde3d1af4fb7f4"},"cell_type":"code","source":"sns.palplot(sns.color_palette(\"RdBu\", 7))\nsns.palplot(sns.color_palette(\"Blues_d\", 7))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0785273fc541cdb2c442b5693e8ee055899c1923"},"cell_type":"markdown","source":"### 7. Let's set the palette to a matplotlib colormap"},{"metadata":{"trusted":true,"_uuid":"737791f09cfa858a87602fcb17a96953ffc735ee"},"cell_type":"code","source":"sns.set_palette(\"Blues_d\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"04fd24c235edfe8b903abd441ff387a8afc68fc1"},"cell_type":"markdown","source":"### 8. Impact on the plot"},{"metadata":{"trusted":true,"_uuid":"68ead4cfe19783677e31bf4b7ba19f486113e290"},"cell_type":"code","source":"plt.figure(figsize = (15,8))\nsns.boxplot(data=df);","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0eaf3c037139d44d978aa5be97ed5be1567afeff"},"cell_type":"markdown","source":"### 9. Building custom color palettes"},{"metadata":{"trusted":true,"_uuid":"519e93cf69bda9f295852d0691c5d7df1de6a160"},"cell_type":"code","source":"my_palette = ['#4B0082', '#0000FF', '#00FF00', '#FFFF00', '#FF7F00', '#FF0000']\nsns.set_palette(my_palette)\nsns.palplot(sns.color_palette())","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"33dd2673277c1f11b4c09511c255c8e92098a2c6"},"cell_type":"markdown","source":"### 10. Let's see how the plot has changed"},{"metadata":{"trusted":true,"_uuid":"6fbf116079bbf7d4d858dc73ed79d15e91e4cf37"},"cell_type":"code","source":"plt.figure(figsize = (15,8))\nsns.boxplot(data=df);","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"cc3381713177b3c63d695118a53ccca7916fbc17"},"cell_type":"markdown","source":"# 22.Controlling plot aesthetics <a id=\"221\"></a>\n---\n[**Go To TOP**](#00)\n\n![](https://tgmstat.files.wordpress.com/2013/11/tips1.png)\n\n### In this section, you can learn\n\n1. First plot with seaborn\n2. Changing the plot style with set_style\n\t1. Set plot background to a white grid\n\t1. Set the plot background to dark\n\t1. Set the background to white\n\t1. Adding 'ticks\n3. Customizing the styles\n\t1. Style parameters\n4. Plotting Context Presets\n\t1. Plotting Context Preset - paper\n\t1. Plotting Preset - talk\n\t1. Plotting Preset - poster"},{"metadata":{"_uuid":"22e56ecfc3f49dd1787b3b4e21089f152188e1ed"},"cell_type":"markdown","source":"### 1. First plot with seaborn"},{"metadata":{"trusted":true,"_uuid":"d3724c36033c696346882ddf49fa2ad3e034cf71"},"cell_type":"code","source":"import pandas as pd\nfrom matplotlib import pyplot as plt\n%matplotlib inline\nimport seaborn as sns\ndf = pd.read_csv('../input/datasetsdifferent-format/data-alcohol.csv')\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f8b4307ad537d4245c7f2192ee5b1de0897ea78e"},"cell_type":"code","source":"sns.distplot(df.beer_servings)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"995ae29fb621054d29813cc4ede7079690306412"},"cell_type":"markdown","source":"### 2. Changing the plot style with set_style\n#### 1. Set plot background to a white grid"},{"metadata":{"trusted":true,"_uuid":"1ea0d67b849067246516084d59e0d8e0ef5279a4"},"cell_type":"code","source":"sns.set()\nsns.set_style(\"whitegrid\")\nsns.lmplot(x='beer_servings', y='wine_servings', data=df);","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"973689716f722ccfd20a8e4c38bc943a1247f3d7"},"cell_type":"markdown","source":"#### 2. Set the plot background to dark"},{"metadata":{"trusted":true,"_uuid":"f52477138027967aca725ff0acff98128532fc86"},"cell_type":"code","source":"sns.set()\nsns.set_style(\"dark\")\nsns.lmplot(x='beer_servings', y='wine_servings', data=df, fit_reg=False);","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0a0bb68407d3b995188f47d834d7b5afcdc0885c"},"cell_type":"markdown","source":"#### 3.Set the background to white"},{"metadata":{"trusted":true,"_uuid":"850e0d73f88dac879f226bdde3b1517348d24655"},"cell_type":"code","source":"# sns.set()\nsns.set_style(\"white\")\nplt.figure(figsize=(15,8))\nsns.swarmplot(x='country', y='wine_servings', data=df);","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8677bc95a455d6e0d804aa366a7dd709f4fb03aa"},"cell_type":"markdown","source":"#### 4.Adding 'ticks"},{"metadata":{"trusted":true,"_uuid":"33f9b624f590f7f993c6c46cfa35e1761daace77"},"cell_type":"code","source":"plt.figure(figsize=(15,8))\nsns.set_style(\"ticks\")\nsns.boxplot(data=df);","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3aa34f5278090d078eafe0faad52188d2e67c3cb"},"cell_type":"markdown","source":"### 3.Customizing the styles\n#### 1.Style parameters"},{"metadata":{"trusted":true,"_uuid":"1ad852fb636a7a16f4c9ea39b19fed8e60bf7f1c"},"cell_type":"code","source":"sns.axes_style()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e842cd83a6c71930e5db29364c83790f6997aae2"},"cell_type":"code","source":"plt.figure(figsize=(15,8))\nsns.set_style(\"ticks\", {\"axes.facecolor\": \".1\"})\nsns.boxplot(data=df);","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9b88183d1c4b6e5354c95cad1d6a69f19c1392de"},"cell_type":"markdown","source":"### 4.Plotting Context Presets\n#### 1.Plotting Context Preset - paper"},{"metadata":{"trusted":true,"_uuid":"5f817e3d91922aee9283ecc8af87a6216d326a1f"},"cell_type":"code","source":"sns.set()\nsns.set_context(\"paper\")\nplt.figure(figsize=(15, 8))\nsns.lmplot(x='beer_servings', y='wine_servings', data=df);","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7582447457a970b35af417e7bfe7fb9e13045e9d"},"cell_type":"markdown","source":"#### 2.Plotting Preset - talk"},{"metadata":{"trusted":true,"_uuid":"9ba82357ef06175b55b23b252f861026631acbde"},"cell_type":"code","source":"sns.set()\nsns.set_context(\"talk\")\nplt.figure(figsize=(8, 6))\nsns.lmplot(x='beer_servings', y='wine_servings', data=df);","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"cb41c074e1dea6e8a1deb766776bfed9cc2f89e8"},"cell_type":"markdown","source":"#### 3.Plotting Preset - poster"},{"metadata":{"trusted":true,"_uuid":"b7c7e9d5f617de2c464e7617dea90c3f519bc702"},"cell_type":"code","source":"sns.set()\nsns.set_context(\"poster\")\nplt.figure(figsize=(8, 6))\nsns.lmplot(x='beer_servings', y='wine_servings', data=df);","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7e8b1c2c4d44cde925e2c9baf4e0d54c29adbca5"},"cell_type":"markdown","source":"# 23.Plotting categorical data <a id=\"231\"></a>\n---\n[**Go To TOP**](#00)\n\n![](https://i.stack.imgur.com/IsxzL.png)\n\n### In this section, you can learn\n\n1. Scatterplots\n2. Swarmplot\n3. Boxplot\n4. Violinplot\n5. Barplot\n6. Countplot\n7. Wide form plots"},{"metadata":{"_uuid":"8705d5125d49dd7a92a7dfc10d49656e21f51bda"},"cell_type":"markdown","source":"#### 1.Scatterplots"},{"metadata":{"trusted":true,"_uuid":"0706495b152a080a101347bd95ef2da581dd72b2"},"cell_type":"code","source":"import pandas as pd\nfrom matplotlib import pyplot as plt\n%matplotlib inline\nimport seaborn as sns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9d0aaf8edb925c4c6de5184a8c22a0effe7b50b4"},"cell_type":"code","source":"df = pd.read_csv('../input/datasetsdifferent-format/data_simpsons_episodes.csv')\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f05550ce139766da3c76323d65c09356ae0d8613"},"cell_type":"code","source":"plt.figure(figsize=(20,8))\nsns.stripplot(x=\"season\", y=\"us_viewers_in_millions\", data=df);","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c2b6ebb786dbd98dd546362a2346b8f0e4dca1fa"},"cell_type":"markdown","source":"#### 2.Swarmplot"},{"metadata":{"trusted":true,"_uuid":"111880843157b6edc22a5ebbf9d06e5686254eeb"},"cell_type":"code","source":"plt.figure(figsize=(20,8))\nsns.swarmplot(x=\"season\", y=\"us_viewers_in_millions\", data=df);","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4d909c671038041d58a4e4b3dca3d0d5bc3bf883"},"cell_type":"markdown","source":"#### 3.Boxplot"},{"metadata":{"trusted":true,"_uuid":"2922ba2d47c810f362c7a1b8e6bb4dc0181e18d2"},"cell_type":"code","source":"plt.figure(figsize=(20,8))\nsns.boxplot(x=\"season\", y=\"us_viewers_in_millions\", data=df);\n# sns.boxenplot(x=\"season\", y=\"us_viewers_in_millions\", data=df);","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0cb7f658bc504b36ad23d51daae2ec472a6bdff4"},"cell_type":"markdown","source":"#### 4.Violinplot"},{"metadata":{"trusted":true,"_uuid":"7972ea45bc24a3470acf6c430820456bc39f617a"},"cell_type":"code","source":"plt.figure(figsize=(20,8))\nsns.violinplot(x=\"season\", y=\"us_viewers_in_millions\", data=df);","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"939f3aa8ba0318087eb295ad06aada3fd7b94980"},"cell_type":"markdown","source":"#### 5.Barplot"},{"metadata":{"trusted":true,"_uuid":"79725418a2049974d70b2e0b595bedf63023c5de"},"cell_type":"code","source":"plt.figure(figsize=(20,8))\nsns.barplot(x=\"season\", y=\"us_viewers_in_millions\", data=df);","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4f09caf12ffa7c248bdbf444a66706a151bef27b"},"cell_type":"markdown","source":"#### 6.Count Plot"},{"metadata":{"trusted":true,"_uuid":"8dffc91ab3a4682d8f7a755f2495727d9ad22deb"},"cell_type":"code","source":"plt.figure(figsize=(20,8))\nsns.countplot(x=\"season\", data=df);","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"62b5f769410b48e96880ffb0c86564dc4934c2dc"},"cell_type":"markdown","source":"#### 7.Wide form plot"},{"metadata":{"trusted":true,"_uuid":"73fa21a03b7b91d7fc20813b242119d3c27b4701"},"cell_type":"code","source":"df = pd.read_csv('../input/datasetsdifferent-format/data-alcohol.csv')\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7d173cd7675a262df02185c117878517cc0bd767"},"cell_type":"code","source":"plt.figure(figsize=(20,8))\nsns.boxplot(data=df, orient=\"h\");","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8ffd9bea4f44ff1da5cd3690ebec622a3bb29937"},"cell_type":"markdown","source":"# 24.Plotting with data aware grids <a id=\"241\"></a>\n---\n[**Go To TOP**](#00)\n\n![](https://i.stack.imgur.com/YsSZc.png)\n\n### In this section, you can learn\n\n1. Plotting with FacetGrid()\n2. Plotting with PairGrid()\n\t1. MLB Players Height, Weight, Age and Positions dataset\n3. Plot it with PairGrid()\n4. Plotting with PairPlot()"},{"metadata":{"_uuid":"09b33f3006fc87ea5a3627dd614ef9d01e23e87f"},"cell_type":"markdown","source":"### 1. Plotting with FacetGrid()"},{"metadata":{"trusted":true,"_uuid":"85228153c49345cdc87a166505302b38e804efad"},"cell_type":"code","source":"df = pd.read_csv('../input/datasetsdifferent-format/data-titanic.csv')\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dd03387f1052a8ed71d0a55ca929ebed8ade473a"},"cell_type":"code","source":"g = sns.FacetGrid(df, col=\"Sex\", hue='Survived')\ng.map(plt.hist, \"Age\");\ng.add_legend();","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"17a570fc4e7f187e4f75baaa8e01c403937c3581"},"cell_type":"markdown","source":"### 2.Plotting with PairGrid()\n#### MLB Players Height, Weight, Age and Positions dataset"},{"metadata":{"trusted":true,"_uuid":"e8e6f1e37a916c9bda843de68fa734b2fe972352"},"cell_type":"code","source":"mlb = pd.read_csv('../input/datasetsdifferent-format/data-mlb-players.csv')\nmlb.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6e4311aa954b72a2f842847d9db7747ef15be449"},"cell_type":"markdown","source":"### 3.Plot it with PairGrid()"},{"metadata":{"trusted":true,"_uuid":"bdb984348110b4e98ee7d5cd1209f0345e7a9389"},"cell_type":"code","source":"g = sns.PairGrid(mlb, vars=[\"Height\", \"Weight\"], hue=\"Position\")\ng.map(plt.scatter);\ng.add_legend();","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e0eb43eabe15ae3a3d8396209892c86338f5001a"},"cell_type":"markdown","source":"### 4.Plot it with PairGrid()"},{"metadata":{"trusted":true,"_uuid":"7805ab03e6e7961c2512338039270392d077dd6c"},"cell_type":"code","source":"sns.pairplot(mlb, hue=\"Position\", size=2.5);","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a955f33ac0871464e0f587912b2153b758a5bc9d"},"cell_type":"markdown","source":"### <span style=\"color:orange\">Thanks for Reading this notebook...🙏...🙏...🙏!!!</span>"}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}