Pandas DataFrame.to_string() method: Explained with examples

Updated: February 20, 2024 By: Guest Contributor Post a comment

Introduction

Pandas is a fast, powerful, flexible, and easy-to-use open-source data analysis and manipulation tool, built on top of the Python programming language. It offers various functions to transform and represent data in different formats. One such method is to_string(), which converts a DataFrame into a printable string format. This tutorial will delve into the to_string() method of the DataFrame object in Pandas, explaining its utility and showcasing its application through various examples.

Using DataFrame.to_string() in Action

The to_string() method is used to render a DataFrame to a console-friendly tabular output. It’s particularly useful when working with large DataFrames, as it allows for the customization of its output for better readability. The method returns a string representation of the DataFrame.

Basic Usage

import pandas as pd

df = pd.DataFrame({
  'A': [1, 2, 3],
  'B': ['a', 'b', 'c']
})

print(df.to_string())

Output:

   A  B
0  1  a
1  2  b
2  3  c

Controlling the Columns Displayed

import pandas as pd

df = pd.DataFrame({
  'A': [1, 2, 3, 4],
  'B': ['a', 'b', 'c', 'd'],
  'C': [True, False, True, False]
})

print(df.to_string(columns=['A', 'B']))

Output:

   A  B
0  1  a
1  2  b
2  3  c
3  4  d

Formatting Float Precision

import pandas as pd

df = pd.DataFrame({
  'A': [1.123456, 2.123456, 3.123456],
  'B': [4.654321, 5.654321, 6.654321]
})

print(df.to_string(float_format="%.2f"))

Output:

     A    B
0 1.12 4.65
1 2.12 5.65
2 3.12 6.65

Handling Large Datasets

import numpy as np
import pandas as pd

np.random.seed(2024)

df = pd.DataFrame(np.random.rand(100, 4), columns=list('ABCD'))

print(df.to_string(max_rows=10))

Output:

           A         B         C         D
0   0.588015  0.699109  0.188152  0.043809
1   0.205019  0.106063  0.727240  0.679401
2   0.473846  0.448296  0.019107  0.752598
3   0.602449  0.961778  0.664369  0.606630
4   0.449151  0.225354  0.670174  0.735767
..       ...       ...       ...       ...
95  0.063296  0.699578  0.282142  0.421581
96  0.612998  0.510631  0.680846  0.981441
97  0.318863  0.113418  0.256580  0.589992
98  0.504235  0.953197  0.509708  0.169022
99  0.268508  0.950527  0.442548  0.703101

Notice that only the first 5 rows and the last 5 rows are displayed to keep the output concise.

Customizing Index

import pandas as pd

df = pd.DataFrame({
  'A': [1, 2, 3],
  'B': ['a', 'b', 'c']
}, index=['x', 'y', 'z'])

print(df.to_string(index=False))

Output:

 A B
 1 a
 2 b
 3 c

Advanced Usage: Custom Index and Float Format

import pandas as pd

df = pd.DataFrame(
    {
        "A": [0.999999, 1.555555, 2.555555],
        "B": ["long_string_value", "another_long_string", "yet_another_long_string"],
    },
    index=["first", "second", "third"],
)

print(df.to_string(formatters={"A": "{:0.2f}".format, "B": lambda x: x[:10]}))

Output:

          A           B
first  1.00  long_strin
second 1.56  another_lo
third  2.56  yet_anothe

When to Use to_string()

The to_string() method is versatile and can be employed in several scenarios, including:

  • Printing a concise summary of a large DataFrame to the console.
  • Logging the state of a DataFrame at a specific point in time.
  • Generating a text representation of a DataFrame to include in reports or emails.

Conclusion

The to_string() method offers great flexibility in rendering DataFrames to string format, accommodating various needs and preferences. Through this tutorial, we’ve explored several examples demonstrating how to utilize this method effectively. Whether working with small or large datasets, to_string() proves to be an invaluable tool in a data scientist’s arsenal.