# Remove samples/rows from anndata

**URL:** <https://discourse.scverse.org/t/remove-samples-rows-from-anndata/959>\
**Category:** anndata\
**Created:** [November 29, 2022, 2:38am UTC](https://discourse.scverse.org/t/remove-samples-rows-from-anndata/959 "2022-11-29T02:38:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamesmo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/jamesmo/32/1012_2.png) [@jamesmo](https://discourse.scverse.org/u/jamesmo)\
**Post date:** [November 29, 2022, 2:38am UTC](https://discourse.scverse.org/t/remove-samples-rows-from-anndata/959/1 "2022-11-29T02:38:53Z")

</div>

I can see how to remove columns from anndata ie

`keep = ['a','b','c']`

`adata = adata [:, keep]`

How does one remove rows/sets of samples from `anndata.obs` and `anndata.x`?

for example remove `adata.obs[Region='reg012']`

`adata.obs`

[![enter image description here](https://canada1.discourse-cdn.com/flex035/uploads/forum11/original/1X/2950b2d09610fda75d72671b0d04561ed13d1bb4.png)](https://i.stack.imgur.com/uzHhR.png)

---

<div class="post-metadata">

**Author:** ![adamgayoso](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/adamgayoso/32/100_2.png) [@adamgayoso](https://discourse.scverse.org/u/adamgayoso)\
**Post date:** [November 29, 2022, 2:43am UTC](https://discourse.scverse.org/t/remove-samples-rows-from-anndata/959/2 "2022-11-29T02:43:38Z")

</div>

Everything is connected, so you can’t remove rows from `.obs` without removing rows from the whole object. In your case you want to do one of the following:

```python
adata[adata.obs["Region"] != "reg012"]
adata[adata.obs.query("Region != 'reg012'").index]
adata[[r != "reg012" for r in adata.obs.Region]]

```

---

<div class="post-metadata">

**Author:** ![jamesmo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/jamesmo/32/1012_2.png) [@jamesmo](https://discourse.scverse.org/u/jamesmo)\
**Post date:** [February 9, 2023, 10:44am UTC](https://discourse.scverse.org/t/remove-samples-rows-from-anndata/959/3 "2023-02-09T10:44:52Z")

</div>

@adamgayoso thanks.  
How does one do the inverse of keep for adata.var\_names?  
ie to exclude based on input list rather than include.  
`exclude = ['A','B','C']`  
`àdata = adata[:, exclude]`

---

<div class="post-metadata">

**Author:** ![adamgayoso](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/adamgayoso/32/100_2.png) [@adamgayoso](https://discourse.scverse.org/u/adamgayoso)\
**Post date:** [February 9, 2023, 6:22pm UTC](https://discourse.scverse.org/t/remove-samples-rows-from-anndata/959/4 "2023-02-09T18:22:03Z")

</div>

```auto
adata = adata[:, ~adata.var_names.isin(exclude)]

```

Tilde is “not”, so this reads like “var names that are not in exclude”

---

<div class="post-metadata">

**Author:** ![afarha5](https://avatars.discourse-cdn.com/v4/letter/a/f0a364/32.png) [@afarha5](https://discourse.scverse.org/u/afarha5)\
**Post date:** [January 9, 2024, 4:44am UTC](https://discourse.scverse.org/t/remove-samples-rows-from-anndata/959/5 "2024-01-09T04:44:38Z")

</div>

Hi there. If I have a list of cluster labels. And I want to remove all the cells in my anndata that have a label that belongs to said list called clusters\_to\_remove. How can I go about doing that? @adamgayoso

---

<div class="post-metadata">

**Author:** ![jamesmo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/jamesmo/32/1012_2.png) [@jamesmo](https://discourse.scverse.org/u/jamesmo)\
**Post date:** [January 9, 2024, 5:02am UTC](https://discourse.scverse.org/t/remove-samples-rows-from-anndata/959/6 "2024-01-09T05:02:20Z")

</div>

clusterstoremove= [‘1’,‘2’,‘3’]  
adata = adata[adata.obs.clustercolumname.isin(clusterstoremove)==FALSE].copy()
