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, default: None) – An optional list of column names that should belong to obs only and not X.

  • index_column (str | None, default: 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")