Mercurial > repos > recetox > table_pandas_transform
comparison table_pandas_rename_column.py @ 0:b722dba91064 draft default tip
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/tables commit d0ff40eb2b536fec6c973c3a9ea8e7f31cd9a0d6
author | recetox |
---|---|
date | Wed, 29 Jan 2025 15:35:51 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:b722dba91064 |
---|---|
1 import argparse | |
2 import logging | |
3 from typing import Tuple | |
4 | |
5 import pandas as pd | |
6 from utils import KeyValuePairsAction, LoadDataAction, StoreOutputAction | |
7 | |
8 | |
9 def rename_columns(df: pd.DataFrame, rename_dict: dict): | |
10 """ | |
11 Rename columns in the dataframe based on the provided dictionary. | |
12 | |
13 Parameters: | |
14 df (pd.DataFrame): The input dataframe. | |
15 rename_dict (dict): A dictionary with 1-based column index as key and new column name as value. | |
16 | |
17 Returns: | |
18 pd.DataFrame: The dataframe with renamed columns. | |
19 """ | |
20 try: | |
21 rename_map = { | |
22 df.columns[key - 1]: value for key, value in rename_dict.items() | |
23 } # Convert 1-based index to column name | |
24 return df.rename(columns=rename_map) | |
25 except IndexError as e: | |
26 logging.error(f"Invalid column index: {e}") | |
27 raise | |
28 except Exception as e: | |
29 logging.error(f"Error renaming columns: {e}") | |
30 raise | |
31 | |
32 | |
33 def main(input_dataset: pd.DataFrame, rename_dict: dict, output_dataset: Tuple[callable, str]): | |
34 """ | |
35 Main function to load the dataset, rename columns, and save the result. | |
36 | |
37 Parameters: | |
38 input_dataset (pd.DataFrame): The input dataset . | |
39 rename_dict (dict): A dictionary with 1-based column index as key and new column name as value. | |
40 output_dataset (tuple): The function to store the output dataset and the path. | |
41 """ | |
42 try: | |
43 write_func, file_path = output_dataset | |
44 write_func(rename_columns(input_dataset, rename_dict), file_path) | |
45 except Exception as e: | |
46 logging.error(f"Error in main function: {e}") | |
47 raise | |
48 | |
49 | |
50 if __name__ == "__main__": | |
51 logging.basicConfig(level=logging.INFO) | |
52 parser = argparse.ArgumentParser(description="Rename columns in a dataframe.") | |
53 parser.add_argument( | |
54 "--input_dataset", | |
55 nargs=2, | |
56 action=LoadDataAction, | |
57 required=True, | |
58 help="Path to the input dataset and its file extension (csv, tsv, parquet)", | |
59 ) | |
60 parser.add_argument( | |
61 "--rename", | |
62 nargs="+", | |
63 action=KeyValuePairsAction, | |
64 required=True, | |
65 help="List of key=value pairs with 1-based column index as key and new column name as value", | |
66 ) | |
67 parser.add_argument( | |
68 "--output_dataset", | |
69 nargs=2, | |
70 action=StoreOutputAction, | |
71 required=True, | |
72 help="Path to the output dataset and its file extension (csv, tsv, parquet)", | |
73 ) | |
74 | |
75 args = parser.parse_args() | |
76 main(args.input_dataset, args.rename, args.output_dataset) |