Data Mapping and Cleanup
pdb.apply_mapping_and_cleanup
Applies an external mapping file to convert IDs in one column of a DataFrame, creates a new column, and optionally performs cleanup on specified columns.
| Parameter | Type | Description |
|---|---|---|
df |
pd.DataFrame | The input DataFrame to be mapped. |
mapping_file_path |
str | Path to the external mapping file (e.g., HGNC file). |
mapping_sep |
str | Separator (delimiter) used in the mapping file (e.g., '\t'). |
origin_col |
str | Name of the column in df containing the IDs to be mapped (the key). |
target_col |
str | Name of the new column to be created with the mapped values. |
origin_ref_col |
str | Name of the column in the mapping file corresponding to the key. |
target_ref_col |
str | Name of the column in the mapping file corresponding to the value. |
cleanup_subset_cols |
Optional[List[str]] | Columns to use for the final dropna operation. Defaults to only target_col. |
origin_col_dtype |
Union[type,str] | Data type hint for preprocessing the origin_col in df. (Currently unused internally, defaults to str conversion). |
origin_ref_dtype |
Union[type,str] | Data type hint for preprocessing the origin_ref_col in the mapping file. (Currently unused internally, defaults to str conversion). |
Returns
pd.DataFrame: The DataFrame with the new target_col added and rows with missing mapped values removed.
Usage
# Map UniProt IDs in 'gene' column to HGNC symbols in new 'symbol' column
df_mapped = pdb.apply_mapping_and_cleanup(
df=hmdb_gc_df,
mapping_file_path="hgnc_complete_set.txt",
mapping_sep="\t",
origin_col="gene",
target_col="symbol",
origin_ref_col="UniProt ID(s)",
target_ref_col="Approved symbol",
cleanup_subset_cols=["gene", "symbol", "compound"],
)