A DataFrame is the primary data structure of the Pandas library and is commonly used for storing and working with tabular data. By default, when you print a DataFrame, it gets printed along with a 0-based row-wise index. A common operation that could be performed on such data is printing the DataFrame without the index.
To start working with Pandas, we first need to import it :
import pandas as pd
Let us understand this operation with the help of an example. Consider the following DataFrame containing 3 students with names A, B, and C and their corresponding marks (out of 10) for two subjects, Mathematics and Physics. By default, this has an index column with zero-based row-wise indices.
Code snippet for generating the above DataFrame :
import pandas as pd
# Dictionary for our data
data = {'Name' : ['A', 'B', 'C'], 'Mathematics' : [8, 5, 10], 'Physics' : [7, 9, 8]}
# DataFrame for the dictionary
df = pd.DataFrame(data)
# Printing the DataFrame
print(df)
Here, ‘data’ is a dictionary we created to initialize the DataFrame. For this, we use the .DataFrame() function of the Pandas library which takes the dictionary as an argument and returns the required DataFrame.
As we saw, just ‘df’ or ‘print(df)’ is a pretty straightforward way of printing the DataFrame. By default, the DataFrame is printed with an initial column containing 0-based index. But, what if we need to print the DataFrame without this column as shown below?
Required Output :
Let us look at different ways to do this :
In this method, we use the to_string() function with the parameter ‘index’ set as False to print the DataFrame without index. By default, if nothing is specified, the value of this ‘index’ parameter is always set to True.
import pandas as pd
# Dictionary for our data
data = {'Name' : ['A', 'B', 'C'], 'Mathematics' : [8, 5, 10], 'Physics' : [7, 9, 8]}
# DataFrame for the dictionary
df = pd.DataFrame(data)
# Printing the DataFrame without index
print(df.to_string(index=False))
Output :
To have a better understanding of what the .to_string() function generates, let us have a look at its output.
NOTE: This output was generated using google colab and this code might not work on some compilers.
import pandas as pd
# Dictionary for our data
data = {'Name' : ['A', 'B', 'C'], 'Mathematics' : [8, 5, 10], 'Physics' : [7, 9, 8]}
# DataFrame for the dictionary
df = pd.DataFrame(data)
# Printing the DataFrame without index
df.to_string(index = False)
Output :
‘Name Mathematics Physics\n A 8 7\n B 5 9\n C 10 8’
For programmer’s still using Python 2.7, let us look at the code you can use to generate the same output :
import pandas as pd
# Dictionary for our data
data = {'Name' : ['A', 'B', 'C'], 'Mathematics' : [8, 5, 10], 'Physics' : [7, 9, 8]}
# DataFrame for the dictionary
df = pd.DataFrame(data)
# Printing the DataFrame without index
print df.to_string(index = False)
Output :
‘Name Mathematics Physics\n A 8 7\n B 5 9\n C 10 8’
In this method, we make the ‘.index’ property of the DataFrame empty by assigning to it a list of empty characters of length equal to the number of rows in the DataFrame.
len(df) gives us the length of the DataFrame, i.e, the number of rows in the DataFrame. When this is multiplied with [‘’], we get a list of empty characters with length equal to the number of rows in the DataFrame.
To print the updated DataFrame without index, we simply write df.
import pandas as pd
# Dictionary for our data
data = {'Name' : ['A', 'B', 'C'], 'Mathematics' : [8, 5, 10], 'Physics' : [7, 9, 8]}
# DataFrame for the dictionary
df = pd.DataFrame(data)
# Making index empty
df.index = [''] * len(df)
# Printing the DataFrame without index
print(df)
Output :
In this method, we use the hide_index() function of the DataFrame’s ‘.style’ property to print the DataFrame without index. This is generally considered to be the best approach for this task for Python 3.7 or later versions.
import pandas as pd
# Dictionary for our data
data = {'Name' : ['A', 'B', 'C'], 'Mathematics' : [8, 5, 10], 'Physics' : [7, 9, 8]}
# DataFrame for the dictionary
df = pd.DataFrame(data)
# Printing the DataFrame without index
df.style.hide_index()
Output :
In this method, we make the index empty with [‘’] for each row at the time of initializing the DataFrame.
len(data) gives us the length of the DataFrame, i.e the number of rows in the DataFrame. Multiplying this with [‘’] gives us a list of empty characters with length equal to the number of rows in the DataFrame. We assign this to the ‘index’ parameter of the function.
pd.DataFrame() is used to initialize the DataFrame. Here we do this with the help of a dictionary ‘data’.
To print the DataFrame we simply write ‘df’.
import pandas as pd
# Dictionary for our data
data = {'Name' : ['A', 'B', 'C'], 'Mathematics' : [8, 5, 10], 'Physics' : [7, 9, 8]}
# DataFrame for the dictionary
df = pd.DataFrame(data)
# Dictionary for our data
data = {'Name' : ['A', 'B', 'C'], 'Mathematics' : [8, 5, 10], 'Physics' : [7, 9, 8]}
# DataFrame for the dictionary
df = pd.DataFrame(data, index = ['']*len(data))
# Printing the DataFrame
print(df)
Output :
In this method, we use the to_html() function from the HTML library from IPython.display, after importing it. This function converts the DataFrame to printable HTML format. If we specify the parameter ‘index’ as False, we can print the DataFrame without index. By default, the ‘index’ parameter is set to True.
import pandas as pd
# Dictionary for our data
data = {'Name' : ['A', 'B', 'C'], 'Mathematics' : [8, 5, 10], 'Physics' : [7, 9, 8]}
# DataFrame for the dictionary
df = pd.DataFrame(data)
# Importing the required library
from IPython.display import HTML
# Printing the DataFrame without index
HTML(df.to_html(index = False))
Output :
In this topic, we have learned to print an existing Pandas DataFrame without index, following a running example of test scores of students in different subjects, thus giving us an intuition of how this concept could be applied in real-world situations. Feel free to explore other methods and reach out to info.javaexercise@gmail.com in case of any suggestions.