Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to display Pandas Dataframe in Python without Index?
In Pandas, DataFrames display with an index by default. You can hide the index when displaying using to_string(index=False) or when printing to console using various methods.
Creating a Sample DataFrame
Let's start by creating a DataFrame with custom index and column labels ?
import pandas as pd
# Create DataFrame with custom index
dataFrame = pd.DataFrame([[10, 15], [20, 25], [30, 35]],
index=['x', 'y', 'z'],
columns=['a', 'b'])
print("DataFrame with default index display:")
print(dataFrame)
DataFrame with default index display:
a b
x 10 15
y 20 25
z 30 35
Method 1: Using to_string(index=False)
The to_string() method converts the DataFrame to a string format without the index ?
import pandas as pd
dataFrame = pd.DataFrame([[10, 15], [20, 25], [30, 35]],
index=['x', 'y', 'z'],
columns=['a', 'b'])
print("DataFrame without index:")
print(dataFrame.to_string(index=False))
DataFrame without index: a b 10 15 20 25 30 35
Method 2: Using print() with to_string()
You can also access specific rows and display them without index ?
import pandas as pd
dataFrame = pd.DataFrame([[10, 15], [20, 25], [30, 35]],
index=['x', 'y', 'z'],
columns=['a', 'b'])
# Select a specific row
print("Selected row 'x':")
print(dataFrame.loc['x'])
print("\nAll data without index:")
print(dataFrame.to_string(index=False))
Selected row 'x': a 10 b 15 Name: x, dtype: int64 All data without index: a b 10 15 20 25 30 35
Method 3: Setting Display Options
For temporary display without index across your session, modify pandas display options ?
import pandas as pd
dataFrame = pd.DataFrame([[10, 15], [20, 25], [30, 35]],
index=['x', 'y', 'z'],
columns=['a', 'b'])
# Temporarily hide index for this display
with pd.option_context('display.show_dimensions', False):
print("Using option context:")
print(dataFrame.to_string(index=False))
Using option context: a b 10 15 20 25 30 35
Comparison
| Method | Use Case | Output Type |
|---|---|---|
to_string(index=False) |
One-time display | String representation |
pd.option_context() |
Temporary session setting | Formatted display |
pd.set_option() |
Permanent session setting | Global formatting |
Conclusion
Use to_string(index=False) for displaying DataFrames without index. This method is most commonly used for clean output presentation while keeping the original DataFrame structure intact.
Advertisements
