Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pandas to Anndata? #600

Closed
rgranit opened this issue Jul 29, 2021 · 2 comments
Closed

Pandas to Anndata? #600

rgranit opened this issue Jul 29, 2021 · 2 comments
Labels

Comments

@rgranit
Copy link

rgranit commented Jul 29, 2021

Hey all,

Is there an option to convert Pandas df to Anndata object?

Thanks,
Roy

@rgranit
Copy link
Author

rgranit commented Jul 29, 2021

I think I might have figured it out, but confirmation would help:

# dataframe for annotating the observations
obs = pd.DataFrame()
obs['sample'] = df.columns
var_names = df.index
# dataframe for annotating the variables
var = pd.DataFrame(index=var_names)
# the data matrix 
X = df.T.values
adata = anndata.AnnData(X, obs=obs, var=var, dtype='int32')

output:
AnnData object with n_obs × n_vars = 10 × 24514 obs: 'sample'

@ivirshup
Copy link
Member

ivirshup commented Nov 9, 2021

You could just pass the dataframe directly to the anndata constructor, but you'll only get X, obs_names, and var_names.

Here's an example:

import scanpy as sc, anndata as ad

pbmc = sc.datasets.pbmc68k_reduced()

pbmc.to_df().head()
index              HES4  TNFRSF4  SSU72  PARK7   RBP7    SRM  MAD2L2  AGTRAP  \
index                                                                          
AAAGCCTGGCTAAC-1 -0.326   -0.191 -0.728 -0.301  3.386 -0.531   2.016   3.377   
AAATTCGATGCACA-1  1.171   -0.191  0.795 -1.200 -0.174 -0.531   1.889  -0.486   
AACACGTGGTCTTT-1 -0.326   -0.191  0.483 -1.200 -0.174 -0.531  -0.451   0.971   
AAGTGCACGTGCTA-1 -0.326   -0.191  1.134 -0.157 -0.174 -0.531  -0.451  -0.486   
ACACGAACGGAGTG-1 -0.326   -0.191 -0.728 -0.607 -0.174 -0.531  -0.451   0.787
...
ad.AnnData(pbmc.to_df())
AnnData object with n_obs × n_vars = 700 × 765

@ivirshup ivirshup closed this as completed Nov 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants