Pandas DataFrame to_string() Method

Rate this post


Before any data manipulation can occur, three (3) new libraries will require installation.

  • The Pandas library enables access to/from a DataFrame.
  • The Pyarrow library allows writing/reading access to/from a parquet file.
  • The Openpyxl library allows styling/writing/reading to/from an Excel file.

To install these libraries, navigate to an IDE terminal. At the command prompt ($), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($). Your terminal prompt may be different.

$ pip install pandas

Hit the <Enter> key on the keyboard to start the installation process.

$ pip install pyarrow

Hit the <Enter> key on the keyboard to start the installation process.

$ pip install openpyxl

Hit the <Enter> key on the keyboard to start the installation process.

If the installations were successful, a message displays in the terminal indicating the same.

Feel free to view the PyCharm installation guide for the required libraries.

Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.

import pandas as pd
import pyarrow
import openpyxl


The to_string() method converts a DataFrame object to a terminal-based tabbed output.

The syntax for this method is as follows:

DataFrame.to_string(buf=None, columns=None, col_space=None, header=True, index=True, na_rep='NaN', formatters=None, float_format=None, sparsify=None, index_names=True, justify=None, max_rows=None, max_cols=None, show_dimensions=False, decimal='.', line_width=None, min_rows=None, max_colwidth=None, encoding=None)

The respective parameters:

bufThis parameter is the file path/buffer to write. If empty, a string returns.
columnsThis parameter is the sub-set of columns to write.
If empty, all columns write.
col_spaceThis depicts the length of each column.
headerThis parameter writes out the column names.
indexThis parameter writes out the row (index) names.
na_repThis parameter represents the string value for missing data.
formattersThis parameter is a formatter function to apply to elements by position/name.
float_formatThis parameter is a formatter for floating-point numbers.
sparsifyIf True and MultiIndex, display the key for each row.
index_namesThis parameter displays the index names.
justifyThis parameter determines the column alignment.
max_rowsThis determines the maximum number of rows to display.
max_colsThis determines the maximum number of columns to display.
show_dimensionsThis parameter displays the dimensions of the DataFrame (total rows/columns).
decimalThis parameter is the decimal separator, comma (,) in Europe.
line_widthThis determines the width to wrap a line in characters.
min_rowsThe rows to display if totals rows > max_rows.
max_colwidthThis determines the maximum width at which to truncate column characters.
encodingA string representation of encoding. The default value is UTF-8.

This example reads in the countries.csv file to a DataFrame. This DataFrame then converts to a string.

πŸ’‘ Note: Click here to save this CSV file. Then move it to the current working directory.

df = pd.read_csv('countries.csv').head(4)
result = df.to_string()
  • Line [1] reads in four (4) rows from the countries.csv file. The output saves to a DataFrame df.
  • Line [2] converts the DataFrame to a string. The output saves to result.
  • Line [3] outputs the result to the terminal.



More Pandas DataFrame Methods

Feel free to learn more about the previous and next pandas DataFrame methods (alphabetically) here:

Also, check out the full cheat sheet overview of all Pandas DataFrame methods.