ehrapy.anndata.df_to_anndata

ehrapy.anndata.df_to_anndata(df, columns_obs_only=None, index_column=None)[source]

Transform a given pandas dataframe into an AnnData object.

Note that columns containing boolean values (either 0/1 or T(t)rue/F(f)alse) will be stored as boolean columns whereas the other non numerical columns will be stored as categorical values.

Parameters:
  • df (DataFrame) – The pandas dataframe to be transformed

  • columns_obs_only (list[str] | None) – An optional list of column names that should belong to obs only and not X

  • index_column (str | None) – The index column of obs. This can be either a column name (or its numerical index in the dataframe) or the index of the dataframe

Return type:

AnnData

Returns:

An AnnData object created from the given pandas dataframe

Examples

>>> import ehrapy as ep
>>> import pandas as pd
>>> df = pd.DataFrame(
...     {
...         "patient_id": ["0", "1", "2", "3", "4"],
...         "age": [65, 72, 58, 78, 82],
...         "sex": ["M", "F", "F", "M", "F"],
...     }
... )
>>> adata = ep.ad.df_to_anndata(df, index_column="patient_id")