diff --git a/__init__.py b/__init__.py index 89c827c..dc92201 100644 --- a/__init__.py +++ b/__init__.py @@ -52,6 +52,7 @@ from .modules.pandas_wrapper.pandas_mul import PandasMul from .modules.pandas_wrapper.pandas_mul_series import PandasMulSeries from .modules.pandas_wrapper.pandas_pow import PandasPow from .modules.pandas_wrapper.pandas_pow_series import PandasPowSeries +from .modules.pandas_wrapper.pandas_replace import PandasReplace from .modules.pandas_wrapper.pandas_save_csv import PandasSaveCSV from .modules.pandas_wrapper.pandas_save_json import PandasSaveJSON from .modules.pandas_wrapper.pandas_select_columns import PandasSelectColumns @@ -126,6 +127,7 @@ NODE_CLASS_MAPPINGS: Dict[str, Type[T]] = { "PandasMode": PandasMode, "PandasMul": PandasMul, "PandasMulSeries": PandasMulSeries, + "PandasReplace": PandasReplace, "PandasPow": PandasPow, "PandasPowSeries": PandasPowSeries, "PandasSaveCSV": PandasSaveCSV, @@ -207,6 +209,7 @@ NODE_DISPLAY_NAME_MAPPINGS: Dict[str, str] = { "PandasMulSeries": "Pandas Mul Series", "PandasPow": "Pandas Pow", "PandasPowSeries": "Pandas Pow Series", + "PandasReplace": "Pandas Replace", "PandasSaveCSV": "Pandas Save CSV", "PandasSaveJSON": "Pandas Save JSON", "PandasSelectColumns": "Pandas Select Columns", diff --git a/docs/reference/node_reference.md b/docs/reference/node_reference.md index 04e6e56..1021df3 100644 --- a/docs/reference/node_reference.md +++ b/docs/reference/node_reference.md @@ -21,6 +21,7 @@ | --- | --- | | [Pandas Drop NA](pandas_dropna.md) | Drops missing values from a pandas DataFrame. | | [Pandas Is NA](pandas_isna.md) | Checks a pandas DataFrame for missing values. | +| [Pandas Replace](pandas_replace.md) | Replaces part or all of a string in each DataFrame cell with a specified string using a wildcard for matching. If you want to remove some characters, then leave the replace_string field blank. | ## Data subset selection | Node | Description | | --- | --- | diff --git a/docs/reference/pandas_replace.md b/docs/reference/pandas_replace.md new file mode 100644 index 0000000..9808eca --- /dev/null +++ b/docs/reference/pandas_replace.md @@ -0,0 +1,19 @@ +# Pandas Replace +Replaces part or all of a string in each DataFrame cell with a specified string using a wildcard for matching. If you want to remove some characters, then leave the replace_string field blank. + +## Input +| Name | Data type | +|---|---| +| dataframe | DataFrame | +| regex | String | +| replacement_string | String | + +## Output +| Data type | +|---| +| DataFrame | + +
+Category: Data cleansing + +ComfyUI Data Analysis Node Reference. © 2025 Hide Inada (HowToSD.com). All rights reserved. diff --git a/modules/pandas_wrapper/pandas_replace.py b/modules/pandas_wrapper/pandas_replace.py new file mode 100644 index 0000000..3cb912e --- /dev/null +++ b/modules/pandas_wrapper/pandas_replace.py @@ -0,0 +1,44 @@ +from typing import Any, Dict +import pandas as pd + + +class PandasReplace: + """ + Replaces part or all of a string in each DataFrame cell with a specified string using a wildcard for matching. If you want to remove some characters, then leave the replace_string field blank. + + category: Data cleansing + """ + + @classmethod + def INPUT_TYPES(cls) -> Dict[str, Any]: + """ + Defines the input types for the function. + + Returns: + Dict[str, Any]: A dictionary specifying required input types. + """ + return { + "required": { + "dataframe": ("DATAFRAME", {}), + "regex": ("STRING", {"default": ""}), + "replacement_string": ("STRING", {"default": ""}) + } + } + + RETURN_TYPES: tuple = ("DATAFRAME",) + FUNCTION: str = "f" + CATEGORY: str = "Data Analysis" + + def f(self, dataframe: pd.DataFrame, regex: str, replacement_string: str) -> tuple: + """ + Replaces part or all of a string in each DataFrame cell with a specified string using a wildcard for matching. + + Args: + dataframe (DataFrame): The target DataFrame. + regex (str): A regular expression pattern. + + Returns: + tuple: A tuple containing the DataFrame. + """ + df2 = dataframe.replace({regex: replacement_string}, regex=True) + return (df2,) diff --git a/modules/pandas_wrapper/utils.py b/modules/pandas_wrapper/utils.py index 1ebf63d..7d39ec7 100644 --- a/modules/pandas_wrapper/utils.py +++ b/modules/pandas_wrapper/utils.py @@ -2,6 +2,7 @@ import json import numpy as np import pandas as pd + def series_to_jsons(series: pd.Series) -> str: """ Convert a pandas Series to a JSON string. diff --git a/pyproject.toml b/pyproject.toml index bf3d364..3e09ef0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [project] name = "data-analysis" description = "Data analysis custom modules for ComfyUI - Use Pandas & Matplotlib within ComfyUI" -version = "1.1.7" +version = "1.1.8" license = {file = "LICENSE"} [project.urls]