Added Pandas Rename node to rename column label or row label (index). Fixed Pandas As Int node's output which was broken due to v1.1.0 refactoring.
This commit is contained in:
@@ -40,4 +40,4 @@ class PandasAsInt:
|
||||
"""
|
||||
# Convert all values to integer, non-numeric values become NaN
|
||||
df = dataframe.apply(pd.to_numeric, errors='coerce').astype('Int64')
|
||||
return (df.to_json(),)
|
||||
return (df,)
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
from typing import Any, Dict
|
||||
import pandas as pd
|
||||
|
||||
|
||||
class PandasRename:
|
||||
"""
|
||||
Renames an index (row label) or a column label.
|
||||
|
||||
category: Transformation
|
||||
"""
|
||||
|
||||
@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", {}),
|
||||
"axis": (("columns", "index"),),
|
||||
"current_label": ("STRING", {"default": ""}),
|
||||
"new_label": ("STRING", {"default": ""}),
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES: tuple = ("DATAFRAME",)
|
||||
FUNCTION: str = "f"
|
||||
CATEGORY: str = "Data Analysis"
|
||||
|
||||
def f(self, dataframe: pd.DataFrame, axis: str, current_label: str, new_label: str) -> tuple:
|
||||
"""
|
||||
Renames an index (row label) or a column label.
|
||||
|
||||
Args:
|
||||
dataframe (DataFrame): The DataFrame.
|
||||
axis (str): Index (row label) or column label.
|
||||
current_label (str): The current label.
|
||||
new_label (str): The new label.
|
||||
|
||||
Returns:
|
||||
tuple: A tuple containing a JSON string of the renamed DataFrame.
|
||||
"""
|
||||
if axis=="index":
|
||||
df_out = dataframe.rename(index={current_label:new_label})
|
||||
elif axis=="columns":
|
||||
df_out = dataframe.rename(columns={current_label:new_label})
|
||||
else:
|
||||
raise ValueError("Invalid axis. Only index or columns are supported.")
|
||||
return (df_out,)
|
||||
Reference in New Issue
Block a user