Bioactivity_Final_Project_QM9 / src /09_unpack_ecfp4.py
Desp-ML's picture
Upload 18 files
43fd9b3 verified
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)