# Slicing DataFrames

You can access subsets of your dataframe (views) in a few different ways, but we will focus on two here.

Name-based indexing

```
You provide a row_index and a column_index - they can be slices or lists or whatever to the .loc[row_names, col_names] indexer
example: [your_dataframe_name].loc[my_row_names, my col_names].
```

Index-based indexing

```
You provide the row and column numbers to the .iloc[row_numbers, col_numbers] 
example: [your_dataframe_name].iloc[my_row_numbers, my col_numbers]
```

```
ms.loc[:,'Protein Name'] #get all row (:), 'Protein Name' column
```

<figure><img src="/files/m7eLXV9mwP7tHiYGYD1Y" alt=""><figcaption></figcaption></figure>

```
#How would you get the first 10 rows using .loc (note that here the row "names" are just numbers
```

```
ms.loc[:9, 'Protein Name']
```

<figure><img src="/files/ePpAtQj7ND71XQjmglJv" alt=""><figcaption></figcaption></figure>

```
ms.loc[0:10,['Protein Name', 'Protein Gene']] #what will this return?
```

<figure><img src="/files/XCXxRZ0mTSdHnS1NKDDt" alt=""><figcaption></figcaption></figure>

```
# Note that you can pass any list of column names to the column indexer
ms.loc[:8,[col for col in ms.columns if "Protein" in col]] #what is this doing?
```

Side topic: get familiar with \[[List Comprehension](https://www.w3schools.com/python/python_lists_comprehension.asp)]

<figure><img src="/files/sLlNiBRsE4I9l811qsij" alt=""><figcaption></figcaption></figure>

```
my_list =[ ]
for col in ms.columns:
    if "Protein" in col:
        my_list.append(col)
```

```
my_list
```

\['Protein Name', 'Protein Preferred Name', 'Protein Gene']

```
my_list = [col for col in ms.columns if "Protein" in col]
```

```
my_list
```

\['Protein Name', 'Protein Preferred Name', 'Protein Gene']

```
ms.loc[:5,my_list] #what is this doing?
```

<figure><img src="/files/rv9oKdfp9sBubJuAgIje" alt=""><figcaption></figcaption></figure>

```
list(ms.columns) #this provides the full list of the columns in the dataframe
```

<figure><img src="/files/3cmtQpd6AbUuaILZgN5v" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/wuZ6l25kv0i20iT3Marh" alt=""><figcaption></figcaption></figure>

```
# write a line to access all columns related to sample BT2_HFX_6
ms.loc[:,[col for col in ms.columns if "BT2_HFX_6" in col]]
```

<figure><img src="/files/lszSat4Rxtv5rPuhpukp" alt=""><figcaption></figcaption></figure>

```
# Now let's try indexing with .iloc
ms.iloc[:5,3:9] #note the difference in how iloc and loc work!>
```

<figure><img src="/files/Ucnx8MV5nTqDnGvPNiFB" alt=""><figcaption></figcaption></figure>

```
ms.iloc[:20,'Precursor Charge'] #Will this work?
```

<figure><img src="/files/lxnnhp3bNqkEI9db3xTP" alt=""><figcaption></figcaption></figure>

```
ms.iloc[:20,4]
```

<figure><img src="/files/Sex76lCtmKwLRucjucCL" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://igb.mit.edu/mini-courses/python/data-processing-with-python/pandas/slicing-dataframes.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
