[FEAT][dataframe_to_text]

pull/393/head
Kye 11 months ago
parent 2c9bed2a41
commit 7aeebff790

@ -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))

@ -46,6 +46,8 @@ from swarms.utils.video_to_frames import (
######## ########
from swarms.utils.yaml_output_parser import YamlOutputParser from swarms.utils.yaml_output_parser import YamlOutputParser
from swarms.utils.pandas_to_str import dataframe_to_text
__all__ = [ __all__ = [
"SubprocessCodeInterpreter", "SubprocessCodeInterpreter",
@ -82,4 +84,5 @@ __all__ = [
"MarkVisualizer", "MarkVisualizer",
"video_to_frames", "video_to_frames",
"save_frames_as_images", "save_frames_as_images",
"dataframe_to_text",
] ]

@ -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))
Loading…
Cancel
Save