Skip to main content

How to Print a pandas DataFrame Without the Index

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

print() on a pandas DataFrame adds a column of row numbers on the left, the index, which is noise when you only want to show the data, for example in a command-line tool's output. to_string(index=False) turns the table into text without it, and float_format rounds its numbers while you're at it. This guide prints a small price list three ways, with real output from pandas 3.1.1.

Syntax​

Syntax
print(frame.to_string(index=False))
print(frame.to_string(index=False, float_format="{:.2f}".format))
PartWhat it means
frame.to_string()Returns the table as one string; "render a DataFrame to a console-friendly tabular output". You print it yourself
index=FalseLeave out the index (row) labels
float_format=...A function applied to every decimal number, such as "{:.2f}".format for two decimal places

Step 1: Print a Table Three Ways​

The example is a price list of two drinks. Create print_table.py:

The print_table.py script in a macOS-style code window: it builds a DataFrame with item and price columns for tea at 2.5 and coffee at 3.14159, then prints it as it is, with to_string(index=False), and with to_string(index=False) plus a float_format that rounds to two decimal places, with an empty line between each.

print_table.py
import pandas as pd

prices = pd.DataFrame({"item": ["tea", "coffee"], "price": [2.5, 3.14159]})

print(prices)
print()
print(prices.to_string(index=False))
print()
print(prices.to_string(index=False, float_format="{:.2f}".format))
Terminal
uv run python print_table.py
Output
item price
0 tea 2.50000
1 coffee 3.14159

item price
tea 2.50000
coffee 3.14159

item price
tea 2.50
coffee 3.14

A terminal running uv run python print_table.py: first the table with index numbers 0 and 1 and prices 2.50000 and 3.14159, then the same without the index, then without the index and with prices 2.50 and 3.14.

Step 2: Read the Three Versions​

  1. print(prices) shows the index, 0 and 1, and gives every price the same number of decimals, five here, because of 3.14159.
  2. to_string(index=False) drops the index. The columns are still lined up.
  3. float_format="{:.2f}".format rounds every decimal number to two places. "{:.2f}".format is a function that turns a number into text with two decimals, which is what float_format expects.

to_string() doesn't change prices: the table keeps its index and its full numbers.

Step 3: Print Every Row of a Long Table​

print() shortens long tables: by default pandas shows at most 60 rows, then only the first and last few with ... between them. to_string() has no such limit unless you pass max_rows. Create long_table.py, which counts the lines each one prints for a table of 100 numbers:

long_table.py
import pandas as pd

numbers = pd.DataFrame({"n": range(100)})

print(len(str(numbers).splitlines()), "lines from print()")
print(len(numbers.to_string().splitlines()), "lines from to_string()")
Terminal
uv run python long_table.py
Output
14 lines from print()
101 lines from to_string()

print() showed 14 lines, ending with [100 rows x 1 columns]; to_string() showed the header and all 100 rows.

Common Mistakes​

MistakeWhat happensDo this instead
Calling prices.to_string(index=False) without print in a scriptNothing appears: it only returns the textprint(prices.to_string(index=False))
Rounding the data to make it print nicelyThe values themselves lose precisionFormat only the printed text with float_format

Troubleshooting​

SymptomLikely causeFix
... in the middle of a printed tableprint() shortens tables longer than 60 rowsPrint frame.to_string()
Whole numbers print with .00float_format applies to float columns, and the column holds floatsStore whole numbers as integers
The table wraps across linesIt's wider than the terminalPass line_width, or print fewer columns

FAQ​

Can I hide the column names too?​

Yes: to_string(index=False, header=False) prints only the values.

Does this work for one column?​

A single column is a Series; series.to_string(index=False) works the same way.

How do I write the table to a file without the index?​

For a CSV file, frame.to_csv("table.csv", index=False): How to Save a pandas DataFrame to a CSV File.

Conclusion​

print(frame.to_string(index=False)) printed the price list without row numbers, and float_format="{:.2f}".format rounded its prices to two places in the output only. Use to_string() whenever a table must print in full and look tidy, such as in a command-line tool.

Additional Resources​