Вы можете использовать iterrows
для итерации по строкам данных, в каждой строке вы можете получить столбцы и распечатать результат так, как вы хотите. Например:
import pandas as pd
dtf = pd.DataFrame({
"Name": ["car", "other"],
"Number": [5, 6],
"Description": ["red", "green"]
})
def stringify_dataframe(dtf):
text = ""
for i, row in dtf.iterrows():
for col in dtf.columns.values:
text += f"{col}: {row[col]}\n"
text += "\n"
return text
s = stringify_dataframe(dtf)
Теперь s
содержит следующее:
>>> print(s)
Name: car
Number: 5
Description: red
Name: other
Number: 6
Description: green