scverse / anndata

Annotated data.
http://anndata.readthedocs.io
BSD 3-Clause "New" or "Revised" License
571 stars 152 forks source link

Include metadata when converting to Pandas DataFrame #1399

Open WeilerP opened 7 months ago

WeilerP commented 7 months ago

Please describe your wishes and possible alternatives to achieve the desired result.

Description

AnnData.to_df only converts data from .X or a layer to a DataFrame. It'd be great to also add metadata column from AnnData.obs. This feature would make it easier to work with Pandas groupby on the generated DataFrame, for example.

ivirshup commented 7 months ago

To me, this kind of use case is covered by sc.get.obs_df. Is there some reason that doesn't do what you need?

ivirshup commented 7 months ago

Alternatively, adata.to_df().join(adata.obs)

WeilerP commented 7 months ago

To me, this kind of use case is covered by sc.get.obs_df. Is there some reason that doesn't do what you need?

It requires Scanpy instead of using AnnData directly. And yes, you can always merge the relevant columns; it'd just be easier via an argument.

ivirshup commented 7 months ago

It requires Scanpy instead of using AnnData directly.

Just to establish an alternative: if sc.get.obs_df instead lived in anndata, that would be sufficient?