# Loosing anndata .var layer when using sc.concat?

**URL:** <https://discourse.scverse.org/t/loosing-anndata-var-layer-when-using-sc-concat/1605>\
**Category:** scanpy\
**Created:** [July 14, 2023, 5:39pm UTC](https://discourse.scverse.org/t/loosing-anndata-var-layer-when-using-sc-concat/1605 "2023-07-14T17:39:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Avaptel18](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/avaptel18/32/448_2.png) [@Avaptel18](https://discourse.scverse.org/u/Avaptel18)\
**Post date:** [July 14, 2023, 5:39pm UTC](https://discourse.scverse.org/t/loosing-anndata-var-layer-when-using-sc-concat/1605/1 "2023-07-14T17:39:33Z")

</div>

Hi, I am merging my samples, after initial QC, with scanpy.concat and l am loosing my .var layer.

This is my code  
adata = sc.concat(adatas, join=‘outer’, index\_unique=“\_”, keys=sample\_ids, label=‘sample\_ID’)

Can I provide any other parameter so that concat function keeps the .var layer.  
Thank you.

---

<div class="post-metadata">

**Author:** ![tavareshugo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/tavareshugo/32/1031_2.png) [@tavareshugo](https://discourse.scverse.org/u/tavareshugo)\
**Post date:** [May 1, 2024, 5:02pm UTC](https://discourse.scverse.org/t/loosing-anndata-var-layer-when-using-sc-concat/1605/2 "2024-05-01T17:02:03Z")

</div>

Duplicate of [this question](https://discourse.scverse.org/t/help-with-concat/676/3).  
I’ve contributed to that thread, but will also answer here as this was the first hit when I was searching for a solution.

I don’t think `sc.concat()` currently (v1.10.1) has an option for this, but it is possible to merge all the `.var` DataFrames and add them to the `.var` of the final concatenated AnnData.

First, some data to have a reproducible example:

```py
import scanpy as sc
import pandas as pd

pbmc = sc.datasets.pbmc68k_reduced()
adatas = {"set1": pbmc[0:5, 0:5], "set2": pbmc[5:10, 0:10], "set3": pbmc[10:15, 0:6]}

```

In this example, we have 3 sets of data stored in a dictionary, with some gene (vars) overlap between them.

We can concatenate our data:

```py
adata = sc.concat(adatas, join="outer", label="set", index_unique="-")

```

After doing this, we loose the `.var` attribute.  
To get it back, we can grab all the `.var` attributes from each set and merge them, as in [this answer](https://discourse.scverse.org/t/help-with-concat/676/2).

```py
# grab all var DataFrames from our dictionary
all_var = [x.var for x in adatas.values()]
# concatenate them
all_var = pd.concat(all_var, join="outer")
# remove duplicates
all_var = all_var[~all_var.duplicated()]

```

Now we add this to our concatenated AnnData, making sure the order of the features is the same:

```py
adata.var = all_var.loc[adata.var_names]

```

---

<div class="post-metadata">

**Author:** ![pcm32](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/pcm32/32/1160_2.png) [@pcm32](https://discourse.scverse.org/u/pcm32)\
**Post date:** [September 9, 2024, 1:51pm UTC](https://discourse.scverse.org/t/loosing-anndata-var-layer-when-using-sc-concat/1605/3 "2024-09-09T13:51:06Z")

</div>

Thanks @tavareshugo , this saved me some time.

I had to do a small change though, in the last line of the all\_var generation, I think that the de-duplication should probably be made based on the index:

```auto
# remove duplicates
all_var = all_var[~all_var.index.duplicated()]

```

otherwise this attempts to get completely different rows, which might not work if you have already metrics for the genes that are dependent of the datasets.

---

<div class="post-metadata">

**Author:** ![tavareshugo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/tavareshugo/32/1031_2.png) [@tavareshugo](https://discourse.scverse.org/u/tavareshugo)\
**Post date:** [September 9, 2024, 2:37pm UTC](https://discourse.scverse.org/t/loosing-anndata-var-layer-when-using-sc-concat/1605/4 "2024-09-09T14:37:53Z")

</div>

Yes, you’re right! I’d done the right thing in the other thread, but mistyped it here. Thanks for the correction!
