> For the complete documentation index, see [llms.txt](https://igb.mit.edu/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://igb.mit.edu/mini-courses/python/data-processing-with-python/pandas/selecting-from-dataframes.md).

# Selecting from DataFrames

You can "search/select" data by generating "boolean" arrays based on some criteria. This works by effectively generating a column of True/False values that Pandas uses to select particular rows (those that are true). There are a few ways to generate this true/false selection column.

### **Value-based selections**

You provide a selection criteria for a particular column. Example:

<pre><code># generates the true/false array
<strong>my_dataframe['my_column']>=some_value
</strong></code></pre>

### **Is-in based selections**

You provide a list of values you want to search for. Example:

```
subset_of_rows = my_dataframe['column_name'].isin([list_of_values])
```

### **Other**

There are lots of ways to do this - you can learn more [here](https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#indexing-boolean)

### Boolean Indexing

```
ms['Precursor Charge']==3
```

This is boolean indexing - you can make very complicated selection criteria to just pull out the data you want

```
selection_criteria = ms['Precursor Charge']==3 #now we have saved the selection criteria
```

```
selection_criteria
```

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

```
ms[selection_criteria] #note that only the "True" rows are selected
```

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

```
ms[ms['Precursor Charge']==3]
```

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

```
# Try to select all of the rows with "light Precursor Mz" greater than 800, and do it in one line.
ms[ms['light Precursor Mz']>800]
```

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

```
ms[ms['Peptide Modified Sequence'].str.contains('Q')][['Protein Preferred Name', 'Peptide Modified Sequence']]
```

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

```
ms[ms['Peptide Modified Sequence'].str.contains('SV')]
```

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

```
# Edit the above to only get peptides with the motif 'SV' and only output interested columns
ms[ms['Peptide Modified Sequence'].str.contains('SV')][['Protein Preferred Name', 'Peptide Modified Sequence']]
```

<figure><img src="/files/0If1SvtNPcTmYZOy6BU9" alt=""><figcaption></figcaption></figure>

```
# now let's try using "isin"
ms[ms['Protein Preferred Name'].isin(['RL27_ECOLI'])]
```

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


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
