Print Pandas DataFrames without Index in Python

Hey fellow Python coder! In this tutorial, we will learn how to print Pandas DataFrames without Indexing using Python Programming language.

Pandas is a powerful library for data manipulation and analysis in Python. You should have Pandas installed in your system. If you have not installed it earlier, do the same using the command : pip install pandas.

Before we begin with the implementation, first let’s understand what we are aiming to achieve by the end of this tutorial. By default when we print DataFrames we see a separate column with the index of each row which doesn’t really provide any relevant information. To picture things even better have a look at the image below.

Printing Pandas DataFrames without Index in Python

So let’s get started on removing the indexing!

Step 1 – Creating DataFrames

We will start off by creating a simple dataFrame using the Pandas library which I hope you are already aware of. If not, then no worries I got you! Have a look at the code below:

import pandas as pd

candyData = {
    'Name': ['ChocoLava', 'GummyBears', 'CaramelCrunch', 'JellyBlast', 'MintyMunch'],
    'Type': ['Chocolate', 'Gummy', 'Caramel', 'Jelly', 'Mint'],
    'Flavor': ['Milk Chocolate', 'Assorted Fruit', 'Butterscotch', 'Mixed Berry', 'Peppermint'],
    'Calories': [150, 120, 180, 100, 90],
    'Sugar(in g)': [15, 10, 20, 8, 5]
}

candyDataFrame = pd.DataFrame(candyData)
print(candyDataFrame)

The code above has a number of steps including importing the module and defining a dictionary for the data of the dataFrame. To make things interesting we will be using data for the candies and converting it into a dataFrame and finally printing it on the screen. The output looks like the output shown below:

Pandas DataFrames without Index

You can clearly see we have an unnamed column with just numbers (index). Do we need it? Not really. We have the dataset ready with us. Let’s explore various methods to remove the indexing from the dataFrame in the upcoming sections.

Step 2 – Removing the Index From the DataFrames

In this section of the tutorial we will be exploring various approaches to achieve removal of indexes from dataframe using Python Programming Language. Let’s have a look at one method at a time.

By the Use of to_string Method

The very first method is to make use of the to_string function which has a parameter called index which you set to either True or False depending on whether you need the index to be visible or not. Have a look at the implementation of the same below:

print(candyDataFrame.to_string(index=False))

The output after executing the line of code is as follows:

Removing the Index From the DataFrames

By the Use of  style Method

The next method is by using the style functionality as shown in the code below.

candyDataFrame.style.hide_index()

The output after executing the line of code is as follows:

style Method

By the Use of  to_csv Method

Lastly, we can also convert the whole dataFrame to CSV in order to remove the index column from the dataFrame. This can be done using the to_csv function which takes a mandatory parameter that is the name of the output file. To drop the index column we will be using the parameter called index which takes a boolean value depending on whether the user requires the indexes to be visible or not.

candyDataFrame.to_csv('NoIndex_candyDataFrame.csv', index=False)
NoIndex_candyDataFrame = pd.read_csv('NoIndex_candyDataFrame.csv', index_col=0)
NoIndex_candyDataFrame.head()

After converting the dataFrame to CSV, we will read the CSV using read_csv along with an optional parameter called index_col which specifies which index should be considered as the Index column. Later on we will be displaying the top 5 rows of the dataFrame using the head function. The final output looks like this:

to_csv Method

Conclusion

So that is all for this tutorial! You can choose whichever method feels right to you and the problem you wish to solve. I hope you were able to understand how to print dataFrame without indexes.

If you liked this tutorial then give the following tutorials a read as well:

  1. Select rows from Pandas Dataframe Based On Column Values
  2. Reorder indexed rows based on a list in Pandas DataFrame – Python
  3. Convert a Dictionary to a Pandas Dataframe in Python

Happy Learning!

Leave a Reply

Your email address will not be published. Required fields are marked *