Pandas DataFrame.to_string-Python
Last Updated :
15 Mar, 2025
Pandas is a powerful Python library for data manipulation, with DataFrame as its key two-dimensional, labeled data structure. It allows easy formatting and readable display of data. DataFrame.to_string() function in Pandas is specifically designed to render a DataFrame into a console-friendly tabular format as a string output. Example:
Python
import pandas as pd
# Creating a sample DataFrame
df = pd.DataFrame({'Weight': [45, 88, 56, 15, 71],'Name': ['Sam', 'Andrea', 'Alex', 'Robin', 'Kia'],'Age': [14, 25, 55, 8, 21]})
print(df.to_string())
Output Weight Name Age
0 45 Sam 14
1 88 Andrea 25
2 56 Alex 55
3 15 Robin 8
4 71 Kia 21
Explanation: This code creates a DataFrame from a dictionary with three columns (Weight, Name, Age), structures it into a tabular format using pd.DataFrame() and converts it into a fully visible string representation with df.to_string().
Syntax
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)
Parameters:
- buf: Buffer to write the output string to (e.g., a file). Defaults to None, which means the output is returned as a string.
- columns: Specifies a subset of columns to include in the output. If None, all columns are printed.
- col_space: Defines the minimum width of each column.
- header: Whether to print column names. Can also accept a list of column name aliases.
- index: Whether to include index labels. Default is True.
- na_rep: String representation for missing values (NaN). Default is ‘NaN’.
- formatters: Dictionary or list of functions to apply to columns for formatting their output.
- float_format: Formatter function to apply specifically to floating-point numbers.
- sparsify: Controls hierarchical index formatting. If False, prints every multi-index key at each row.
- index_names: Whether to print index names. Default is True.
- justify: Alignment of column headers (‘left’, ‘right’, ‘center’, ‘justify’ or ‘justify-all’).
- max_rows: Maximum number of rows to display. If exceeded, truncates output.
- max_cols: Maximum number of columns to display. If exceeded, truncates output.
- show_dimensions: If True, displays the shape (rows x columns) of the DataFrame.
- decimal: Specifies the character for decimal separation (e.g., ‘,’ for European formatting).
- line_width: Defines the maximum character width of a row before wrapping text.
Returns: The function returns a str containing the formatted DataFrame.
Examples
1. Excluding index labels
Python
import pandas as pd
# Creating a sample DataFrame
df = pd.DataFrame({'Weight': [45, 88, 56, 15, 71],'Name': ['Sam', 'Andrea', 'Alex', 'Robin', 'Kia'],'Age': [14, 25, 55, 8, 21]})
print(df.to_string(index=False))
Output Weight Name Age
45 Sam 14
88 Andrea 25
56 Alex 55
15 Robin 8
71 Kia 21
Explanation: to_string(index=False) method removes the default index labels from the output. Instead of displaying row indices (0, 1, 2, etc.), only the column values are printed in a structured format.
2. Customizing Missing Values Representation
Python
import pandas as pd
# Creating a DataFrame with missing values
df_missing = pd.DataFrame({'A': [12, 4, 5, None, 1],'B': [7, 2, 54, 3, None],'C': [20, 16, 11, 3, 8],'D': [14, 3, None, 2, 6]})
print(df_missing.to_string(na_rep='Missing'))
Output A B C D
0 12.0 7.0 20 14.0
1 4.0 2.0 16 3.0
2 5.0 54.0 11 Missing
3 Missing 3.0 3 2.0
4 1.0 Missing 8 6.0
Explanation: When dealing with missing values (NaN), the na_rep=’Missing’ argument replaces them with the string “Missing”. This ensures better readability and prevents confusion with empty spaces or default NaN values.
Example 3. Custom Formatting for Floating-Point Numbers
Python
import pandas as pd
# Creating a DataFrame with floating-point numbers
df = pd.DataFrame({'Value': [3.14159265, 2.718281828,1.618033988]})
res = df.to_string(float_format="{:.2f}".format)
print(res)
Output Value
0 3.14
1 2.72
2 1.62
Explanation: float_format=”{:.2f}”.format argument rounds floating-point numbers to two decimal places. It ensures numerical data is formatted cleanly, making it easier to interpret without excessive decimal precision.
Example 4. Limiting the Number of Rows and Columns
Python
import pandas as pd
# Creating a sample DataFrame
df = pd.DataFrame({'Weight': [45, 88, 56, 15, 71],'Name': ['Sam', 'Andrea', 'Alex', 'Robin', 'Kia'],'Age': [14, 25, 55, 8, 21]})
print(df.to_string(max_rows=3, max_cols=2))
Output Weight ... Age
0 45 ... 14
.. ... ... ..
4 71 ... 21
Explanation: max_rows=3 and max_cols=2 arguments limit the number of rows and columns displayed. If the DataFrame exceeds these limits, Pandas inserts ellipses (…) to indicate truncated data.
Similar Reads
Pandas DataFrame mean() Method
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas DataFrame mean()Â Pandas dataframe.mean() function returns the mean of the value
2 min read
Python | Pandas dataframe.median()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas dataframe.median() function return the median of the values for the requested a
2 min read
Python | Pandas Series.std()
Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.std() function return sample
2 min read
Apply function to every row in a Pandas DataFrame
Python is a great language for performing data analysis tasks. It provides a huge amount of Classes and functions which help in analyzing and manipulating data more easily. In this article, we will see how we can apply a function to every row in a Pandas Dataframe. Apply Function to Every Row in a P
7 min read
Joining two Pandas DataFrames using merge()
The merge() function is designed to merge two DataFrames based on one or more columns with matching values. The basic idea is to identify columns that contain common data between the DataFrames and use them to align rows. Let's understand the process of joining two pandas DataFrames using merge(), e
4 min read
Python | Pandas DataFrame.astype()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. DataFrame.astype() method is used to cast a pandas object to a specified dtype.astype(
4 min read
Python | Pandas DataFrame.set_index()
Pandas DataFrame.set_index() method sets one or more columns as the index of a DataFrame. It can accept single or multiple column names and is useful for modifying or adding new indices to your DataFrame. By doing so, you can enhance data retrieval, indexing, and merging tasks. Syntax: DataFrame.set
3 min read
Pandas DataFrame.reset_index()
In Pandas, reset_index() method is used to reset the index of a DataFrame. By default, it creates a new integer-based index starting from 0, making the DataFrame easier to work with in various scenarios, especially after performing operations like filtering, grouping or multi-level indexing. Example
3 min read
Python | Pandas Dataframe.at[ ]
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas at[] is used to return data in a dataframe at the passed location. The passed l
2 min read
Pandas DataFrame iterrows() Method
iterrows() method in Pandas is a simple way to iterate over rows of a DataFrame. It returns an iterator that yields each row as a tuple containing the index and the row data (as a Pandas Series). This method is often used in scenarios where row-wise operations or transformations are required. Exampl
4 min read