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

14.9k views Asked by At

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.

3

There are 3 answers

0
Jafar Isbarov On

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')
0
YotamW Constantini On

Straight forward solution:

adata = sc.AnnData(counts_df)
0
felixm On

Here's my answer that works with scanpy 1.9.1

adata = sc.AnnData(df, 
    df.index.to_frame(), 
    df.columns.to_frame())

Second entry is cell names, third entry is gene names.