# How to concatenate anndata properly?

**URL:** <https://discourse.scverse.org/t/how-to-concatenate-anndata-properly/887>\
**Category:** anndata\
**Tags:** scrna-seq, integration, scvi\
**Created:** [November 2, 2022, 4:08am UTC](https://discourse.scverse.org/t/how-to-concatenate-anndata-properly/887 "2022-11-02T04:08:28Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![PauBadiaM](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/paubadiam/32/410_2.png) [@PauBadiaM](https://discourse.scverse.org/u/PauBadiaM)\
**Post date:** [November 2, 2022, 9:12am UTC](https://discourse.scverse.org/t/how-to-concatenate-anndata-properly/887/2 "2022-11-02T09:12:57Z")

</div>

Hi @cookiemonster,

As a general rule, you should always perform quality control (QC) for each sample individually first. Each sample can be quite different from the rest, requiring different filtering thresholds, for example at the doublet score level. Once you have performed QC at the sample level, you can merge the samples into a single object using the `concatenate` method. By the way, I would recommend to add `join='outer'` to the concatenation because otherwise you might lose quite some genes (by default is set to inner).

Regarding the `n_cells` problem, is this related to your previous question?

> [@How to quantify cell type using scanpy?](https://discourse.scverse.org/t/how-to-quantify-cell-type-using-scanpy/834):
>
> Is there a way to find counts per cell type and the its percentage? I tried to run sc.pl.count\_occurrence(adata, add\_percentage=True, count\_variable='cell\_type') but says scanpy doesn’t have the attribute.

If yes, it is very weird that you store this information into the `var` attribute of your `adata` object, since there it should only store metadata for your features (in this case genes). If what you want is to obtain the number of cells per sample and cell type, you can do it after concatenating your samples instead of doing it before:

```auto
adata.obs[["Sample", "louvain"]].value_counts().reset_index()

```

Since this results in a dataframe that its dimensions are not number of cells x samples (`obs`) nor number of cells x genes (`var`), if you want to store it you could do it in the `uns` attribute:

```auto
adata.uns['n_cells'] = adata.obs[["Sample", "louvain"]].value_counts().reset_index()

```

Hope this is helpful!

```auto

```

---

_[View the full topic](https://discourse.scverse.org/t/how-to-concatenate-anndata-properly/887)._
