diff --git a/__init__.py b/__init__.py index 26b3c21..489bcfc 100644 --- a/__init__.py +++ b/__init__.py @@ -3,6 +3,8 @@ from .modules.matplotlib.mpl_bar import MPLBar from .modules.matplotlib.mpl_line import MPLLine 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_head import PandasHead from .modules.pandas_wrapper.pandas_index import PandasIndex from .modules.pandas_wrapper.pandas_join import PandasJoin @@ -23,6 +25,8 @@ NODE_CLASS_MAPPINGS: Dict[str, Type[T]] = { "MPLLine": MPLLine, "MPLScatter": MPLScatter, "PandasColumns": PandasColumns, + "PandasCreate": PandasCreate, + "PandasCreateWithIndex": PandasCreateWithIndex, "PandasHead": PandasHead, "PandasIndex": PandasIndex, "PandasJoin": PandasJoin, @@ -46,6 +50,8 @@ NODE_DISPLAY_NAME_MAPPINGS: Dict[str, str] = { "MPLLine": "MPL Line Plot", "MPLScatter": "MPL Scatter Plot", "PandasColumns": "Pandas Columns", + "PandasCreate": "Pandas Create", + "PandasCreateWithIndex": "Pandas Create with Index", "PandasHead": "Pandas Head", "PandasIndex": "Pandas Index", "PandasJoin": "Pandas Join", diff --git a/docs/images/create.png b/docs/images/create.png new file mode 100644 index 0000000..398eeba Binary files /dev/null and b/docs/images/create.png differ diff --git a/docs/images/summary.png b/docs/images/summary.png new file mode 100644 index 0000000..5d0c1ac Binary files /dev/null and b/docs/images/summary.png differ diff --git a/docs/ug.md b/docs/ug.md index 33ba102..5f34f24 100644 --- a/docs/ug.md +++ b/docs/ug.md @@ -15,6 +15,8 @@ You can right-click, select **Add Node**, go to **Data Analysis**, and look for |-------------------------|-------------------------------------| | **Pandas Load CSV** | Load a CSV file | | **Pandas Save CSV** | Save a DataFrame to a CSV file | +| **Pandas Create** | Create a DataFrame from CSV text field | +| **Pandas Create with Index** | Create a DataFrame from CSV text field containing index | | **Pandas Columns** | Extract column labels | | **Pandas Index** | Extract row labels (index) | | **Pandas Select Columns** | Select specific columns from a DataFrame | @@ -23,7 +25,7 @@ You can right-click, select **Add Node**, go to **Data Analysis**, and look for | **Pandas Head** | Extract the first few rows | | **Pandas Show DataFrame** | Display DataFrame contents | | **Pandas Show Text** | Display Text | -| **Pandas Summary** | Show DataFrame statistics | +| **Pandas Summary** | Extract DataFrame statistics | | **Pandas Sort** | Sort DataFrame by a column | | **Pandas To String** | Convert DataFrame to a string | | **MPL Bar Chart** | Generate a bar chart | @@ -44,11 +46,17 @@ CSV file path is relative to the ComfyUI installation directory unless you speci ## Saving data Use **Pandas Save CSV** node to save the DataFrame to a CSV file. -## Extracting column and row labels -Use **Pandas Columns** and **Pandas Index** nodes. As these nodes outputs JSON-serialized Index object, to display the value, use **Pandas Show Text** node as shown below: +## Creating DataFrame on the UI +Use **Pandas Create** or **Pandas Create with Index** nodes. These nodes display the multiline text area where you can enter the data in CSV format. +If the data contains an index column, use **Pandas Create with Index** and specify the index column. ![Pandas Columns and Pandas Index](images/columns_index.png) +## Extracting column and row labels +Use **Pandas Columns** and **Pandas Index** nodes. As these nodes output JSON-serialized Index object, to display the value, use **Pandas Show Text** node as shown below: + +![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. @@ -91,8 +99,9 @@ This node has been copied to display text without requiring the user to install ## Displaying DataFrame contents Use **Pandas Show DataFrame** node. When the number of rows is large, Pandas automatically hides middle rows during string conversion, so the display size should be always manageable. -## Showing Show DataFrame statistics -Use **Pandas Summary**. +## Extracting DataFrame statistics +Use **Pandas Summary** to extract statistics then connect the output to **Pandas Show Text** for display. +![Pandas Summary](images/summary.png) ## Sorting DataFrame by a column Use **Pandas Sort**. diff --git a/examples/workflows/create.json b/examples/workflows/create.json new file mode 100644 index 0000000..f04ad21 --- /dev/null +++ b/examples/workflows/create.json @@ -0,0 +1 @@ +{"last_node_id":4,"last_link_id":2,"nodes":[{"id":1,"type":"PandasCreate","pos":[-326.9039306640625,1636.5106201171875],"size":[210,138.73008728027344],"flags":{},"order":0,"mode":0,"inputs":[],"outputs":[{"name":"STRING","type":"STRING","links":[1],"slot_index":0}],"properties":{"Node name for S&R":"PandasCreate"},"widgets_values":["A,B\n1,10\n2,20\n3,30"]},{"id":4,"type":"PandasShowDataFrame","pos":[-64.19639587402344,1834.85595703125],"size":[217.840576171875,134.7733612060547],"flags":{},"order":3,"mode":0,"inputs":[{"name":"dataframe_json","type":"STRING","link":2,"widget":{"name":"dataframe_json"}}],"outputs":[{"name":"STRING","type":"STRING","links":null,"shape":6}],"properties":{"Node name for S&R":"PandasShowDataFrame"},"widgets_values":[""," A B\nUS 1 10\nFrance 2 20\nGermany 3 30"]},{"id":3,"type":"PandasCreateWithIndex","pos":[-324.8951416015625,1834.365478515625],"size":[210,134.33523559570312],"flags":{},"order":1,"mode":0,"inputs":[],"outputs":[{"name":"STRING","type":"STRING","links":[2],"slot_index":0}],"properties":{"Node name for S&R":"PandasCreateWithIndex"},"widgets_values":["index,A,B\nUS,1,10\nFrance,2,20\nGermany,3,30",0]},{"id":2,"type":"PandasShowDataFrame","pos":[-64.8188705444336,1635.102783203125],"size":[216.83184814453125,135.75343322753906],"flags":{},"order":2,"mode":0,"inputs":[{"name":"dataframe_json","type":"STRING","link":1,"widget":{"name":"dataframe_json"}}],"outputs":[{"name":"STRING","type":"STRING","links":null,"shape":6}],"properties":{"Node name for S&R":"PandasShowDataFrame"},"widgets_values":[""," A B\n0 1 10\n1 2 20\n2 3 30"]}],"links":[[1,1,0,2,0,"STRING"],[2,3,0,4,0,"STRING"]],"groups":[],"config":{},"extra":{"ds":{"scale":1.5305009021021516,"offset":[477.1330786503176,-1565.7063254190393]},"node_versions":{"data-analysis":"3e5a6d6efce2f08933edccaf9e1c1e38896df00b"},"ue_links":[]},"version":0.4} \ No newline at end of file diff --git a/modules/pandas_wrapper/pandas_create.py b/modules/pandas_wrapper/pandas_create.py new file mode 100644 index 0000000..f7184f2 --- /dev/null +++ b/modules/pandas_wrapper/pandas_create.py @@ -0,0 +1,46 @@ +from typing import Any, Dict +from io import StringIO +import pandas as pd + + +class PandasCreate: + """ + PandasLoadCSV: + A class for creating a pandas DataFrame using values entered in the text field. + """ + + @classmethod + def INPUT_TYPES(cls) -> Dict[str, Any]: + """ + Defines the input types for the `create` function. + + Returns: + Dict[str, Any]: A dictionary specifying required input types. + """ + return { + "required": { + "data": ("STRING", {"multiline": True}), + } + } + + RETURN_TYPES: tuple = ("STRING",) + FUNCTION: str = "create" + CATEGORY: str = "Data Analysis" + + def create(self, data: str) -> tuple: + """ + Create a pandas DataFrame using values entered in the text field and converts it to a JSON string. + + Args: + file_path (str): The path to the CSV file. + + Returns: + tuple: A tuple containing a JSON string representation of the DataFrame. + """ + # Read CSV file into DataFrame + df = pd.read_csv(StringIO(data)) + + # Convert DataFrame to JSON + result_json = df.to_json() + + return (result_json,) diff --git a/modules/pandas_wrapper/pandas_create_with_index.py b/modules/pandas_wrapper/pandas_create_with_index.py new file mode 100644 index 0000000..a14b2c2 --- /dev/null +++ b/modules/pandas_wrapper/pandas_create_with_index.py @@ -0,0 +1,49 @@ +from typing import Any, Dict +from io import StringIO +import pandas as pd + + +class PandasCreateWithIndex: + """ + PandasLoadCSV: + A class for creating a pandas DataFrame using values entered in the text field. + Input data is assume to have an index. + """ + + @classmethod + def INPUT_TYPES(cls) -> Dict[str, Any]: + """ + Defines the input types for the `create` function. + + Returns: + Dict[str, Any]: A dictionary specifying required input types. + """ + return { + "required": { + "data": ("STRING", {"multiline": True}), + "index_col": ("INT", {"default": 0, "min": 0, "max": 10000}) + } + } + + RETURN_TYPES: tuple = ("STRING",) + FUNCTION: str = "create" + CATEGORY: str = "Data Analysis" + + def create(self, data: str, index_col: int=0) -> tuple: + """ + Create a pandas DataFrame using values entered in the text field and converts it to a JSON string. + Input data is assume to have an index. + + Args: + file_path (str): The path to the CSV file. + + Returns: + tuple: A tuple containing a JSON string representation of the DataFrame. + """ + # Read CSV file into DataFrame + df = pd.read_csv(StringIO(data), index_col=index_col) + + # Convert DataFrame to JSON + result_json = df.to_json() + + return (result_json,) diff --git a/pyproject.toml b/pyproject.toml index 1c2ccd0..e1fe96c 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.6" +version = "1.0.7" license = {file = "LICENSE"} [project.urls]