Added Pandas Group By node.

This commit is contained in:
HowToSD
2025-02-16 17:17:33 -08:00
parent 2b217ab5cb
commit 0aa9586d8b
8 changed files with 92 additions and 2 deletions
+3
View File
@@ -6,6 +6,7 @@ from .modules.matplotlib.mpl_scatter import MPLScatter
from .modules.pandas_wrapper.pandas_columns import PandasColumns
from .modules.pandas_wrapper.pandas_create import PandasCreate
from .modules.pandas_wrapper.pandas_create_with_index import PandasCreateWithIndex
from .modules.pandas_wrapper.pandas_group_by import PandasGroupBy
from .modules.pandas_wrapper.pandas_head import PandasHead
from .modules.pandas_wrapper.pandas_iloc_row_series import PandasIlocRowSeries
from .modules.pandas_wrapper.pandas_iloc_rows_dataframe import PandasIlocRowsDataFrame
@@ -36,6 +37,7 @@ NODE_CLASS_MAPPINGS: Dict[str, Type[T]] = {
"PandasColumns": PandasColumns,
"PandasCreate": PandasCreate,
"PandasCreateWithIndex": PandasCreateWithIndex,
"PandasGroupBy": PandasGroupBy,
"PandasHead": PandasHead,
"PandasIlocRowSeries": PandasIlocRowSeries,
"PandasIlocRowsDataFrame": PandasIlocRowsDataFrame,
@@ -69,6 +71,7 @@ NODE_DISPLAY_NAME_MAPPINGS: Dict[str, str] = {
"PandasColumns": "Pandas Columns",
"PandasCreate": "Pandas Create",
"PandasCreateWithIndex": "Pandas Create With Index",
"PandasGroupBy": "Pandas Group By",
"PandasHead": "Pandas Head",
"PandasIlocRowSeries": "Pandas Iloc Row Series",
"PandasIlocRowsDataFrame": "Pandas Iloc Rows DataFrame",
Binary file not shown.

After

Width:  |  Height:  |  Size: 161 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 134 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 97 KiB

+22 -1
View File
@@ -36,9 +36,10 @@ You can right-click, select **Add Node**, go to **Data Analysis**, and look for
| **Pandas Loc Cell Str** | Select a cell by row label (index) and column label |
| **Pandas Head** | Extract the first few rows |
**Manipulating data**
**Transforming data**
| Node Name | Functionality |
|-------------------------|-------------------------------------|
| **Pandas Group By** | Group rows and apply aggregation functions |
| **Pandas Join** | Join two DataFrames |
| **Pandas Sort** | Sort DataFrame by a column |
@@ -120,6 +121,26 @@ Use **Pandas Loc Cell Str** for selecting a cell by row label (index) and colum
This node outputs the cell value as a string irrespective of the actual cell data type.
## Grouping Rows
Use **Pandas Group By** to group rows and apply aggregation functions.
Currently below aggregation functions are supported:
* sum
* mean
* count
* std
* min
* max
Below shows an example of using sum, mean, count functions:
![Group By](images/group_by1.png)
Below shows an example of using std, min, max functions:
![Group By](images/group_by2.png)
You can also have multiple columns to apply aggregation functions. In the below example, Qty and Price columns are aggregated for the Fruit column:
![Group By](images/group_by3.png)
## Joining DataFrames
Use **Pandas Join**. You can select from inner, left, right, outer joins.
First, use two **Pandas Load CSV** nodes to load two CSVs for the left DataFrame and right DataFrame.
File diff suppressed because one or more lines are too long
+65
View File
@@ -0,0 +1,65 @@
from typing import Any, Dict
from io import StringIO
import pandas as pd
class PandasGroupBy:
"""
PandasGroupBy:
A class for computing aggregate values for groups in a DataFrame.
"""
@classmethod
def INPUT_TYPES(cls) -> Dict[str, Any]:
"""
Defines the input types for the function.
See https://pandas.pydata.org/docs/reference/groupby.html for aggragate functions.
Returns:
Dict[str, Any]: A dictionary specifying required input types.
"""
return {
"required": {
"dataframe_json": ("STRING", {"multiline": True}),
"column_name": ("STRING", {"default": ""}),
"aggragate_function":(("sum", "mean", "count", "std", "min", "max"),)
}
}
RETURN_TYPES: tuple = ("STRING",)
FUNCTION: str = "groupby_dataframes"
CATEGORY: str = "Data Analysis"
def groupby_dataframes(self,
dataframe_json: str,
column_name: str,
aggragate_function: str) -> tuple:
"""
Compute aggregate values for groups in a DataFrame
Args:
dataframe_json (str): A JSON string representation of the DataFrame.
column_name (str): The name of the column for use as groups.
aggregate_function (str): The type of aggregate function to perform.
Returns:
tuple: A tuple containing a JSON string of the merged DataFrame.
"""
# Deserialize JSON string to DataFrame
df = pd.read_json(StringIO(dataframe_json))
if aggragate_function == "max":
df_out = df.groupby(column_name).max()
elif aggragate_function == "min":
df_out = df.groupby(column_name).min()
elif aggragate_function == "std":
df_out = df.groupby(column_name).std()
elif aggragate_function == "count":
df_out = df.groupby(column_name).count()
elif aggragate_function == "sum":
df_out = df.groupby(column_name).sum()
elif aggragate_function == "mean":
df_out = df.groupby(column_name).mean()
else:
raise ValueError("Unsupported aggregate function.")
return (df_out.to_json(),)
+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.0.15"
version = "1.0.16"
license = {file = "LICENSE"}
[project.urls]