Added Pandas Loc Rrow Multi Index DataFrame node.

This commit is contained in:
HowToSD
2025-02-20 22:39:09 -08:00
parent 87a864a867
commit c1c14c4001
7 changed files with 98 additions and 1 deletions
+3
View File
@@ -51,6 +51,7 @@ from .modules.pandas_wrapper.pandas_load_csv_with_index import PandasLoadCSVWith
from .modules.pandas_wrapper.pandas_load_html import PandasLoadHTML
from .modules.pandas_wrapper.pandas_load_json import PandasLoadJSON
from .modules.pandas_wrapper.pandas_loc_cell_str import PandasLocCellStr
from .modules.pandas_wrapper.pandas_loc_row_multiindex_dataframe import PandasLocRowMultiIndexDataFrame
from .modules.pandas_wrapper.pandas_loc_row_series import PandasLocRowSeries
from .modules.pandas_wrapper.pandas_log import PandasLog
from .modules.pandas_wrapper.pandas_max import PandasMax
@@ -154,6 +155,7 @@ NODE_CLASS_MAPPINGS: Dict[str, Type[T]] = {
"PandasLoadHTML": PandasLoadHTML,
"PandasLoadJSON": PandasLoadJSON,
"PandasLocCellStr": PandasLocCellStr,
"PandasLocRowMultiIndexDataFrame": PandasLocRowMultiIndexDataFrame,
"PandasLocRowSeries": PandasLocRowSeries,
"PandasLog": PandasLog,
"PandasMax": PandasMax,
@@ -257,6 +259,7 @@ NODE_DISPLAY_NAME_MAPPINGS: Dict[str, str] = {
"PandasLoadHTML": "Pandas Load HTML",
"PandasLoadJSON": "Pandas Load JSON",
"PandasLocCellStr": "Pandas Loc Cell Str",
"PandasLocRowMultiIndexDataFrame": "Pandas Loc Row Multi Index DataFrame",
"PandasLocRowSeries": "Pandas Loc Row Series",
"PandasLog": "Pandas Log",
"PandasMax": "Pandas Max",
Binary file not shown.

After

Width:  |  Height:  |  Size: 105 KiB

+1
View File
@@ -49,6 +49,7 @@
| [Pandas Iloc Row Series](pandas_iloc_row_series.md) | Selects a row from a pandas DataFrame and returning it as a Series. |
| [Pandas Iloc Rows Data Frame](pandas_iloc_rows_dataframe.md) | Selects rows from a pandas DataFrame and returning it as a DataFrame. |
| [Pandas Loc Cell Str](pandas_loc_cell_str.md) | Selects a cell from a pandas DataFrame and output as a string. |
| [Pandas Loc Row Multi Index Data Frame](pandas_loc_row_multiindex_dataframe.md) | Selects a row from a pandas DataFrame with MultiIndex and output as a DataFrame. |
| [Pandas Loc Row Series](pandas_loc_row_series.md) | Selects a row from a pandas DataFrame and output as a Series. |
| [Pandas Select Columns](pandas_select_columns.md) | Selects specific columns from a pandas DataFrame. |
| [Pandas Select Rows](pandas_select_rows.md) | Selects specific rows from a pandas DataFrame based on a condition. |
@@ -0,0 +1,26 @@
# Pandas Loc Row Multi Index Data Frame
Selects a row from a pandas DataFrame with MultiIndex and output as a DataFrame.
Specify comma-separated MultiIndex labels (row index) and the data type for the MultiIndex to select a row.
Below screenshots illustrate these options:
![PandasLocRowMultiIndexDataFrame](../images/multiindex.png)
The top DataFrame has an integer-type MultiIndex, while the bottom DataFrame has a string-type MultiIndex. In both cases, two index values are specified to select a single row while the correct MultiIndex data type is also selected. Note that mixed data types for MultiIndex are not currently supported.
## Input
| Name | Data type |
|---|---|
| dataframe | DataFrame |
| row_index | String |
| row_index_type | |
## Output
| Data type |
|---|
| DataFrame |
<HR>
Category: Data subset selection
ComfyUI Data Analysis Node Reference. © 2025 Hide Inada (HowToSD.com). All rights reserved.
File diff suppressed because one or more lines are too long
@@ -0,0 +1,66 @@
from typing import Any, Dict
import pandas as pd
class PandasLocRowMultiIndexDataFrame:
"""
Selects a row from a pandas DataFrame with MultiIndex and output as a DataFrame.
Specify comma-separated MultiIndex labels (row index) and the data type for the MultiIndex to select a row.
Below screenshots illustrate these options:
![PandasLocRowMultiIndexDataFrame](../images/multiindex.png)
The top DataFrame has an integer-type MultiIndex, while the bottom DataFrame has a string-type MultiIndex. In both cases, two index values are specified to select a single row while the correct MultiIndex data type is also selected. Note that mixed data types for MultiIndex are not currently supported.
category: Data subset selection
"""
@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", {}),
"row_index": ("STRING", {"default": ""}),
"row_index_type":(("string", "int"),)
}
}
RETURN_TYPES: tuple = ("DATAFRAME",)
FUNCTION: str = "f"
CATEGORY: str = "Data Analysis"
def f(self, dataframe: pd.DataFrame,
row_index: str,
row_index_type: str) -> tuple:
"""
Selects a specific row using a comma-separated MultiIndex labels (row index) from a pandas DataFrame, and returns the DataFrame.
Args:
dataframe (DataFrame): The DataFrame.
row_index (str): A comma-separated MultiIndex label for the row.
row_index_type (str): The data type of the row index.
Returns:
tuple: A tuple containing the Pandas DataFrame.
"""
# We need to pass a tuple of MultiIndex.
# e.g. if the first index is A, second is B, we need to pass:
# ("A", "B")
ind_list = [ind.strip() for ind in row_index.split(",")]
# This will throw an exception if the MultiIndex is not an int.
if row_index_type == "int":
ind_list = [int(ind) for ind in ind_list]
ind_tuple = tuple(ind_list)
# We need to wrap it to get the DataFrame. Otherwise, we will get a Series.
ind_tuple_list = [ind_tuple]
value = dataframe.loc[ind_tuple_list]
return (value,)
+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.15"
version = "1.1.16"
license = {file = "LICENSE"}
[project.urls]