diff --git a/playground/utils/pandas_to_str.py b/playground/utils/pandas_to_str.py new file mode 100644 index 00000000..fccf84eb --- /dev/null +++ b/playground/utils/pandas_to_str.py @@ -0,0 +1,11 @@ +import pandas as pd +from swarms import dataframe_to_text + +# # Example usage: +df = pd.DataFrame({ + 'A': [1, 2, 3], + 'B': [4, 5, 6], + 'C': [7, 8, 9], +}) + +print(dataframe_to_text(df)) diff --git a/swarms/utils/__init__.py b/swarms/utils/__init__.py index de16f1ef..6527d7c1 100644 --- a/swarms/utils/__init__.py +++ b/swarms/utils/__init__.py @@ -46,6 +46,8 @@ from swarms.utils.video_to_frames import ( ######## from swarms.utils.yaml_output_parser import YamlOutputParser +from swarms.utils.pandas_to_str import dataframe_to_text + __all__ = [ "SubprocessCodeInterpreter", @@ -82,4 +84,5 @@ __all__ = [ "MarkVisualizer", "video_to_frames", "save_frames_as_images", + "dataframe_to_text", ] diff --git a/swarms/utils/pandas_to_str.py b/swarms/utils/pandas_to_str.py new file mode 100644 index 00000000..64415487 --- /dev/null +++ b/swarms/utils/pandas_to_str.py @@ -0,0 +1,49 @@ +import pandas as pd + + +def dataframe_to_text( + df: pd.DataFrame, + parsing_func: callable = None, +) -> str: + """ + Convert a pandas DataFrame to a string representation. + + Args: + df (pd.DataFrame): The pandas DataFrame to convert. + parsing_func (callable, optional): A function to parse the resulting text. Defaults to None. + + Returns: + str: The string representation of the DataFrame. + + Example: + >>> df = pd.DataFrame({ + ... 'A': [1, 2, 3], + ... 'B': [4, 5, 6], + ... 'C': [7, 8, 9], + ... }) + >>> print(dataframe_to_text(df)) + + """ + # Get a string representation of the dataframe + df_str = df.to_string() + + # Get a string representation of the column names + info_str = df.info() + + # Combine the dataframe string and the info string + text = f"DataFrame:\n{df_str}\n\nInfo:\n{info_str}" + + if parsing_func: + text = parsing_func(text) + + return text + + +# # # Example usage: +# df = pd.DataFrame({ +# 'A': [1, 2, 3], +# 'B': [4, 5, 6], +# 'C': [7, 8, 9], +# }) + +# print(dataframe_to_text(df))