<jupyter_start><jupyter_text>Selecting Subsets of Data from DataFrames with `iloc`The `iloc` indexer is very similar to the `loc` indexer but only uses **integer location** to make its subset selections. The word `iloc` itself stands for integer location so that should help remind you what it does. Simultaneous row and column subset selectionThe `iloc` indexer is capable of making simultaneous row and column selections just like `loc`. Selection with `iloc` takes on the following form with a comma separating the row and column selections.```pythondf.iloc[rows, cols]```Let's read in some sample data and then begin making selections with integer location using `iloc`.<jupyter_code>import pandas as pd
df = pd.read_csv('../data/sample_data.csv', index_col=0)
df<jupyter_output><empty_output><jupyter_text>Use a list for both rows and columnsLet's select rows with integer location 2 and 4 along with the first and last columns. It is possible to use negative integers in the same manner as Python lists. The integer location -1 refers to the last column below.<jupyter_code>rows = [2, 4]
cols = [0, -1]
df.iloc[rows, cols]<jupyter_output><empty_output><jupyter_text>The possible types of selections for `iloc`In the above example, we used a list of integers for both the row and column selection. You are not limited to just lists. All of the following are valid objects available for both row and column selections with `iloc`.  The `iloc` indexer, unlike `loc`, is unable to do boolean selection. * A single integer* A list of integers* A slice with integers Slice the rows and use a list for the columnsLet's use slice notation to select rows with integer location 2 and 3 and a list to select columns with integer location 4 and 2. Notice that the stop integer location is **excluded** with `iloc`, which is exactly how slicing works with Python lists, tuples, and strings. Slicing with `loc` is **inclusive** of the stop label.<jupyter_code>cols = [4, 2]
df.iloc[2:4, cols]<jupyter_output><empty_output><jupyter_text>Use a list for the rows and a slice for the columnsIn this example, we use a list for the row selection and slice notation for the columns.<jupyter_code>rows = [5, 2, 4]
df.iloc[rows, 3:]<jupyter_output><empty_output><jupyter_text>Select all of the rows and some of the columnsYou can use an empty slice (just the colon) to select all of the rows or columns. In the example below, we select all of the rows and some of the columns with a list.<jupyter_code>cols = [2, 4]
df.iloc[:, cols]<jupyter_output><empty_output><jupyter_text>Cannot do this with *just the brackets**Just the brackets* does select columns, but it only understands **labels** and not **integer location**. The following produces an error as pandas is looking for column names that are the integers 2 and 4.<jupyter_code>df[cols]<jupyter_output><empty_output><jupyter_text>Select some of the rows and all of the columnsWe can again use an empty slice, but do so for the columns to select all of them. Below we use a list to select some of the rows.<jupyter_code>rows = [-3, -1, -2]
df.iloc[rows, :]<jupyter_output><empty_output><jupyter_text>It is possible to rewrite the above without the column selection. pandas defaults to selecting all of the columns if a selection for them is not explicitly present.<jupyter_code>df.iloc[rows]<jupyter_output><empty_output><jupyter_text>Select a single row and a single columnWe can select a single value in our DataFrame using `iloc` by providing a single integer for both the row and column selection. This returns the actual value by itself completely outside of a DataFrame or Series.<jupyter_code>df.iloc[3, 2]<jupyter_output><empty_output><jupyter_text>Select a single row and a single column as a DataFrameIt is possible to select the above value as a DataFrame by using one-item lists for the row and column selections. The output looks a little bizarre, but it's just a DataFrame with one row and one column.<jupyter_code>rows = [3]
cols = [2]
df.iloc[rows, cols]<jupyter_output><empty_output><jupyter_text>Select some rows and a single columnIn this example, a list of integers is used for the rows and a single integer for the columns. pandas returns a Series when a single integer is used to select either a row or column.<jupyter_code>rows = [2, 3, 5]
cols = 4
df.iloc[rows, cols]<jupyter_output><empty_output><jupyter_text>Select a single row or column as a DataFrame and NOT a SeriesYou can select a single row (or column) and return a DataFrame and not a Series if you use a list to make the selection. Let's replicate the selection from the previous example, but use a one-item list for the column selection.<jupyter_code>rows = [2, 3, 5]
cols = [4]
df.iloc[rows, cols]<jupyter_output><empty_output><jupyter_text>Select a single row as a SeriesWe can select a single row by providing a single integer as the row selection for `iloc`. We use an empty slice to select all of the columns. Because we are selecting a single row, a Series is returned. Just as with `loc`, the returned output can be confusing as the original horizontal row is now displayed vertically.<jupyter_code>df.iloc[2, :]<jupyter_output><empty_output><jupyter_text>To maintain the original orientation, we can select the row using a one-item list which returns a DataFrame.<jupyter_code>df.iloc[[2], :]<jupyter_output><empty_output><jupyter_text>Summary of `iloc`The `iloc` indexer is analogous to `loc` but only uses **integer location** for selection. The official pandas documentation refers to it as selection by **position**.* Uses only integer location* Selects rows and columns simultaneously with `df.iloc[rows, cols]`* Selection can be a     * single integer    * a list of integers    * a slice of integers* A comma separates row and column selections ExericsesRead in the movie dataset by executing the cell below and use it for the following exercises.<jupyter_code>pd.set_option('display.max_columns', 50)
movie = pd.read_csv('../data/movie.csv', index_col='title')
movie.head(3)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07179487179487179, 'fraction_numerical': 0.00727874276261373, 'mean_word_length': 5.3441762854144805, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '300547', 'license': 'MIT', 'n_tokens_mistral': 1764, 'n_tokens_neox': 1694, 'n_words': 847, 'path': 'jupyter_notebooks/pandas/mastering_data_analysis/02. Selecting Subsets of Data/03. Selecting Subsets of Data from DataFrames with iloc.ipynb', 'repo_name': 'manual123/Nacho-Jupyter-Notebooks', 'size': '6045'}