How do I create AnnData object from a pandas data frame count matrix?

Viewed 5568

I am trying to use the Scanpy Python package to analyze some single-cell data. I read a count matrix (a .tsv file) in as a Pandas data frame, which has genes as the columns and rows as the different cells. Each row contains the counts for the different genes for a single cell. I would like to create an AnnData object from the Pandas data frame... does anyone know how I can do this? Unfortunately, I cannot provide the dataset.

2 Answers

You can convert your DataFrame df into AnnData adata this way:

adata = anndata.AnnData(X: df.iloc[1:,1:],
                        obs: df.iloc[:,0:1],
                        var: df.iloc[0:1,:])

But you don't really need to do that. Instead, directly read the tsv file into an AnnData object:

with open("your_tsv_file.tsv") as your_data:
    adata = anndata.read_csv(your_data, delimiter='\t')

Straight forward solution:

adata = sc.AnnData(counts_df)
Related