Pandas DataFrame.to_stringPython (original) (raw)
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.