pandas
DataFrame
Python
print
index

How to print pandas DataFrame without index

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Pandas includes the index in most text representations because the index is part of the DataFrame structure. When you want cleaner console output, a plain-text report, or exported text without row labels, the fix is usually to choose the right rendering method rather than modifying the DataFrame itself.

Use to_string(index=False) for console output

If your goal is to print a DataFrame without row numbers in a terminal or log, the most direct approach is to_string(index=False).

python
1import pandas as pd
2
3df = pd.DataFrame(
4    {
5        "name": ["Ada", "Grace", "Linus"],
6        "score": [95, 99, 91],
7    }
8)
9
10print(df.to_string(index=False))

Output:

text
1 name  score
2  Ada     95
3Grace     99
4Linus     91

This leaves the DataFrame unchanged. You are only controlling how it is rendered.

Use to_csv(index=False) when exporting

If you are writing the table somewhere else, such as a file or a response body, use the format-specific export method with index=False.

python
1import pandas as pd
2
3df = pd.DataFrame(
4    {
5        "city": ["Toronto", "Paris"],
6        "population_m": [2.9, 2.1],
7    }
8)
9
10csv_text = df.to_csv(index=False)
11print(csv_text)

This prints:

text
city,population_m
Toronto,2.9
Paris,2.1

That same flag works when writing directly to disk:

python
df.to_csv("cities.csv", index=False)

Reset the index only if the data model should change

Some developers try to hide the index by calling reset_index(). That changes the DataFrame itself by moving the index into a normal column and creating a new default index.

python
normalized = df.reset_index(drop=True)
print(normalized)

This can be useful when the old index no longer has meaning, but it is not the right tool if you only want cleaner output. In most cases, formatting methods are safer because they do not alter the underlying structure.

Notebook and rich-display options

In Jupyter, display behavior can be different from plain print. If you want a notebook-friendly representation without an index, a Styler can help for presentation use cases.

python
1import pandas as pd
2
3df = pd.DataFrame(
4    {
5        "product": ["Book", "Pen"],
6        "price": [12.5, 1.8],
7    }
8)
9
10styled = df.style.hide(axis="index")
11styled

That is useful for reports and notebooks, but it is presentation-specific. For scripts, CLI tools, and logs, to_string(index=False) remains the most direct option.

Other text formats support the same idea

If the output target is Markdown rather than plain text or CSV, pandas also lets you suppress the index there.

python
1import pandas as pd
2
3df = pd.DataFrame({"name": ["Ada", "Grace"], "score": [95, 99]})
4print(df.to_markdown(index=False))

The general pattern is consistent: keep the DataFrame unchanged and control index visibility at the rendering step.

Decide based on the output target

A useful rule is:

  • use to_string(index=False) for human-readable console output
  • use to_csv(index=False) or to_markdown(index=False) for exported text
  • use style.hide(axis="index") for notebook display

Choosing based on the destination keeps the code straightforward and prevents accidental changes to the DataFrame itself.

Common Pitfalls

One common mistake is using print(df) and expecting pandas display settings to hide the index automatically. The default representation includes it, so you need to call an explicit formatting method.

Another mistake is mutating the DataFrame with reset_index() when you only wanted a cosmetic change. That can affect later joins, selections, or assumptions in downstream code.

It is also easy to confuse console printing with export behavior. to_string(index=False) affects only the string representation you print, while to_csv(index=False) affects serialized output. Use the method that matches your destination.

Finally, wide DataFrames may still wrap or truncate depending on display settings. Hiding the index does not solve all formatting problems, so you may also need to adjust pandas display options or choose another export format.

Summary

  • Use df.to_string(index=False) to print a DataFrame without the index.
  • Use df.to_csv(index=False) when exporting text or files.
  • Use notebook styling methods when the goal is visual presentation.
  • Avoid reset_index() unless you truly want to change the DataFrame structure.
  • Pick the formatting method based on where the output is going.

Course illustration
Course illustration

All Rights Reserved.