diff --git a/__init__.py b/__init__.py index 1146b5d..4c891f4 100644 --- a/__init__.py +++ b/__init__.py @@ -6,6 +6,7 @@ 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_head import PandasHead +from .modules.pandas_wrapper.pandas_iloc_row_series import PandasIlocRowSeries from .modules.pandas_wrapper.pandas_index import PandasIndex from .modules.pandas_wrapper.pandas_join import PandasJoin from .modules.pandas_wrapper.pandas_load_csv import PandasLoadCSV @@ -33,6 +34,7 @@ NODE_CLASS_MAPPINGS: Dict[str, Type[T]] = { "PandasCreate": PandasCreate, "PandasCreateWithIndex": PandasCreateWithIndex, "PandasHead": PandasHead, + "PandasIlocRowSeries": PandasIlocRowSeries, "PandasIndex": PandasIndex, "PandasJoin": PandasJoin, "PandasLoadCSV": PandasLoadCSV, @@ -63,6 +65,7 @@ NODE_DISPLAY_NAME_MAPPINGS: Dict[str, str] = { "PandasCreate": "Pandas Create", "PandasCreateWithIndex": "Pandas Create With Index", "PandasHead": "Pandas Head", + "PandasIlocRowSeries": "Pandas Iloc Row Series", "PandasIndex": "Pandas Index", "PandasJoin": "Pandas Join", "PandasLoadCSV": "Pandas Load CSV", diff --git a/docs/images/iloc_row_series.png b/docs/images/iloc_row_series.png new file mode 100644 index 0000000..9152d9e Binary files /dev/null and b/docs/images/iloc_row_series.png differ diff --git a/docs/ug.md b/docs/ug.md index cddebae..65494c4 100644 --- a/docs/ug.md +++ b/docs/ug.md @@ -23,6 +23,7 @@ You can right-click, select **Add Node**, go to **Data Analysis**, and look for | **Pandas Index** | Extract row labels (index) | | **Pandas Select Columns** | Select specific columns from a DataFrame | | **Pandas Select Rows** | Filter rows based on conditions | +| **Pandas Iloc Row Series** | Select a row by row integer position | | **Pandas Loc Row Series** | Select a row by row label (index) | | **Pandas Loc Cell Str** | Select a cell by row label (index) and column label (index) | | **Pandas Join** | Join two DataFrames | @@ -63,14 +64,23 @@ Use **Pandas Columns** and **Pandas Index** nodes. As these nodes output JSON-s ![Pandas Create and Pandas Create With Index](images/create.png) ## Selecting columns and rows -Use **Pandas Select Columns** for selecting columns, **Pandas Select Rows** to select rows by a filter condition. +Use **Pandas Select Columns** for selecting columns. +For rows, there are multiple options. +Use **Pandas Select Rows** to select rows by a filter condition. ![Pandas select subset](images/subset.jpg) -You can use **Pandas Loc Row Series** to select a row by row label (index). This node returns a JSON-serialized Series object. Data type of the labels needs to be specified as shown below: +You can use **Pandas Loc Row Series** to select a row by row label (index), or you can use **Pandas Iloc Row Series** to select a row by row integer position. Both nodes returns a JSON-serialized Series object. +For **Pandas Loc Row Series**, data type of the labels needs to be specified as shown below: ![Pandas Loc Row Series](images/loc_row_series.png) +For **Pandas Iloc Row Series**, you only need to specify the row integer position: +![Pandas Iloc Row Series](images/iloc_row_series.png) + +Note that in Pandas, integer position is an equivalent of an index in other software, +while "index" means a unique label assigned to each row or column. See [pandas.DataFrame.iloc.html](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.iloc.html). + ## Selecting a cell by row label (index) and column label (index) Use **Pandas Loc Cell Str** for selecting a cell by row label (index) and column label (index). Data type of the labels needs to be specified as shown below: diff --git a/modules/pandas_wrapper/pandas_iloc_row_series.py b/modules/pandas_wrapper/pandas_iloc_row_series.py new file mode 100644 index 0000000..5c06b81 --- /dev/null +++ b/modules/pandas_wrapper/pandas_iloc_row_series.py @@ -0,0 +1,50 @@ +from typing import Any, Dict +from io import StringIO +import pandas as pd +from .utils import series_to_jsons + +class PandasIlocRowSeries: + """ + PandasLocRowSeries: + A class for selecting a row from a pandas DataFrame and returning it as a Series. + The row is specified by its integer position, similar to an index in other software. + Note: In Pandas, "index" refers to a unique label assigned to each row or column. See https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.iloc.html + """ + + @classmethod + def INPUT_TYPES(cls) -> Dict[str, Any]: + """ + Defines the input types for the `select_row` function. + + Returns: + Dict[str, Any]: A dictionary specifying required input types. + """ + return { + "required": { + "dataframe_json": ("STRING", {"multiline": True}), + "row_integer_position": ("INT", {"default": 0, "min": 0, "max": 2**31}) + } + } + + RETURN_TYPES: tuple = ("STRING",) + FUNCTION: str = "select_row" + CATEGORY: str = "Data Analysis" + + def select_row(self, dataframe_json: str, + row_integer_position: int) -> tuple: + """ + Selects a specific row using a row label (row index) from a pandas DataFrame, + and returns the JSON serialized Series object. + + Args: + dataframe_json (str): A JSON string representation of the DataFrame. + row_integer_position (int): The row integer position. + + Returns: + tuple: A tuple containing the value of the row in string. The string is + a JSON string containing the serialized Pandas Series object. + """ + # Deserialize JSON string to DataFrame + df = pd.read_json(StringIO(dataframe_json)) + value = df.iloc[row_integer_position] + return (series_to_jsons(value),) diff --git a/pyproject.toml b/pyproject.toml index a54ca06..04aa882 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [project] name = "data-analysis" description = "Data analysis custom modules for ComfyUI - Use Pandas & Matplotlib within ComfyUI" -version = "1.0.12" +version = "1.0.13" license = {file = "LICENSE"} [project.urls]