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
+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.