Added Pandas Replace to replace part or whole string in each DataFrame cell using a regular expression.

This commit is contained in:
HowToSD
2025-02-19 22:58:54 -08:00
parent eaef2d220a
commit 7597dcedfd
6 changed files with 69 additions and 1 deletions
+3
View File
@@ -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",
+1
View File
@@ -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 |
| --- | --- |
+19
View File
@@ -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 |
<HR>
Category: Data cleansing
ComfyUI Data Analysis Node Reference. © 2025 Hide Inada (HowToSD.com). All rights reserved.
+44
View File
@@ -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,)
+1
View File
@@ -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.
+1 -1
View File
@@ -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]