Table of Contents
Printing a Pandas DataFrame without displaying the index can be achieved by using the “index=False” parameter in the “to_string()” method. This will remove the default index column from the printed output and only display the data columns in the DataFrame. This is useful when the index is not relevant or desired to be shown in the printed output.
Print Pandas DataFrame with No Index
You can use the following methods to print a pandas DataFrame without the index:
Method 1: Use to_string() Function
print(df.to_string(index=False))
Method 2: Create Blank Index Before Printing
df.index=[''] * len(df) print(df)
Both methods will print the DataFrame without the index.
The following examples show how to use each method with the following pandas DataFrame:
import pandas as pd
#create DataFrame
df = pd.DataFrame({'team': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],
'points': [18, 22, 19, 14, 14, 11, 20, 28],
'assists': [5, 7, 7, 9, 12, 9, 9, 4],
'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})
#view DataFrame
print(df)
team points assists rebounds
0 A 18 5 11
1 B 22 7 8
2 C 19 7 10
3 D 14 9 6
4 E 14 12 6
5 F 11 9 5
6 G 20 9 9
7 H 28 4 12
Notice that the DataFrame contains an index with values ranging from 0 to 7.
Example 1: Use to_string() Function
The following code shows how to use the to_string() function to print the DataFrame without the index:
#print DataFrame without index
print(df.to_string(index=False))
team points assists rebounds
A 18 5 11
B 22 7 8
C 19 7 10
D 14 9 6
E 14 12 6
F 11 9 5
G 20 9 9
H 28 4 12
Notice that all four columns are printed without the index column.
Example 2: Create Blank Index Before Printing
The following code shows how to first create an index column with all blank values and then print the DataFrame:
#define index to have all blank values
df.index=[''] * len(df)
#print DataFrame
print(df)
team points assists rebounds
A 18 5 11
B 22 7 8
C 19 7 10
D 14 9 6
E 14 12 6
F 11 9 5
G 20 9 9
H 28 4 12
Notice that all four columns are printed without the index column.
The following tutorials explain how to perform other common tasks in pandas:
Cite this article
stats writer (2024). How can I print a Pandas DataFrame without displaying the index?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-print-a-pandas-dataframe-without-displaying-the-index/
stats writer. "How can I print a Pandas DataFrame without displaying the index?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-print-a-pandas-dataframe-without-displaying-the-index/.
stats writer. "How can I print a Pandas DataFrame without displaying the index?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-print-a-pandas-dataframe-without-displaying-the-index/.
stats writer (2024) 'How can I print a Pandas DataFrame without displaying the index?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-print-a-pandas-dataframe-without-displaying-the-index/.
[1] stats writer, "How can I print a Pandas DataFrame without displaying the index?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I print a Pandas DataFrame without displaying the index?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
