# Subset on adata.uns\["X"\].var - help please!

**URL:** <https://discourse.scverse.org/t/subset-on-adata-uns-x-var-help-please/2262>\
**Category:** anndata\
**Created:** [May 8, 2024, 3:23pm UTC](https://discourse.scverse.org/t/subset-on-adata-uns-x-var-help-please/2262 "2024-05-08T15:23:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![spatts14](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/spatts14/32/1034_2.png) [@spatts14](https://discourse.scverse.org/u/spatts14)\
**Post date:** [May 8, 2024, 3:23pm UTC](https://discourse.scverse.org/t/subset-on-adata-uns-x-var-help-please/2262/1 "2024-05-08T15:23:38Z")

</div>

Hi, I am using an AnnData object with lots of layers. In one of the uns. layers, it has another AnnData called “drug2cell”. This drug2cell then has a .var that contains a list of drugs. I would like to subset the list of drugs in this section, but keep it as an AnnData object. Essentially, I am trying to filter my AnnData object.

I would like to filter the dataframe adata.uns[‘drug2cell’].var column called [“drugs\_present”] based on the df

I am trying to only keep .var rows that exist within a specific data frame. However, I need to keep it as an AnnData structure, not convert into a dataframe.

What I have tried already:

to\_keep = df  
adata\_new = adata.uns[‘drug2cell’].var[adata.uns[‘drug2cell’].var[“drugs\_present”].isin(to\_keep)]

however it returns a dataframe, rather than an AnnData

---

<div class="post-metadata">

**Author:** ![gtca](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/gtca/32/1199_2.png) [@gtca](https://discourse.scverse.org/u/gtca)\
**Post date:** [May 8, 2024, 10:44pm UTC](https://discourse.scverse.org/t/subset-on-adata-uns-x-var-help-please/2262/2 "2024-05-08T22:44:18Z")

</div>

Hi @spatts14,

Welcome to our forum 🙌

Since `adata.var` is a dataframe, it makes sense that subsetting it returns a dataframe. You can subset the anndata object itself though:

```python
drugs_to_keep = adata.uns["drug2cell"].var["drugs_present"].isin(to_keep)
adata_new_view = adata.uns["drug2cell"][:,drugs_to_keep]
# ^ this is a view, you can .copy() it to the new object

```

For views vs copies it might be helpful to check out [e.g. this section of the documentation](https://anndata.readthedocs.io/en/latest/tutorials/notebooks/getting-started.html#Views-and-copies).
