Download src/09_unpack_ecfp4.py from Desp-ML/Bioactivity_Final_Project_QM9: direct link, hf CLI and curl.
- Browser
- Download file 1.52 kB
-
https://huggingface.co/datasets/Desp-ML/Bioactivity_Final_Project_QM9/resolve/main/src/09_unpack_ecfp4.py
- Command line
-
hf download hf://datasets/Desp-ML/Bioactivity_Final_Project_QM9/src/09_unpack_ecfp4.py
-
curl -L -o 09_unpack_ecfp4.py https://huggingface.co/datasets/Desp-ML/Bioactivity_Final_Project_QM9/resolve/main/src/09_unpack_ecfp4.py
1.52 kB
| import pandas as pd | |
| def expand_array_column(df: pd.DataFrame, column_name: str, prefix: str = None) -> pd.DataFrame: | |
| """ | |
| Expand a column of sequence-like values into multiple scalar columns. | |
| Parameters: | |
| - df: pandas DataFrame with a column of list/array-like entries. | |
| - column_name: name of the column to expand. | |
| - prefix: optional prefix for new columns; defaults to column_name. | |
| Returns: | |
| - A new DataFrame with the original column dropped and new columns added. | |
| """ | |
| # Extract the list/array values | |
| sequences = df[column_name].tolist() | |
| if not sequences: | |
| raise ValueError(f"Column '{column_name}' is empty.") | |
| # Determine vector length | |
| vec_length = len(sequences[0]) | |
| # Use provided prefix or fallback to column name | |
| prefix = prefix or column_name | |
| new_column_names = [f"{prefix}_{i}" for i in range(vec_length)] | |
| # Build expanded DataFrame | |
| expanded_df = pd.DataFrame(sequences, index=df.index, columns=new_column_names) | |
| # Drop original column and concatenate | |
| df_dropped = df.drop(columns=[column_name]) | |
| result_df = pd.concat([df_dropped, expanded_df], axis=1) | |
| return result_df | |
| df=pd.read_parquet("./ecfp_and_properties/all_data_merged-cleaned-ecfp4-properties-sorted-columns.parquet") | |
| result_df=expand_array_column(df,"Ecfp_4","ECFP") | |
| output_file="./ecfp_and_properties/all_data_merged-cleaned-ecfp4-properties-sorted-columns-expanded-ecfp.parquet" | |
| result_df.to_parquet(output_file, index=False) |