Python

Pandas Add Index

Pandas is a library. We can describe it as being “open-source” and created largely for rationally and quickly transferring the relational or tagged data. It provides a variety of information formats and working methods for handling the time series and numerical data. Pandas are speedy and provide the consumers with excellent performance and reliability. DataFrames in “Pandas” allow us to store information in a tabular format. We can also add “index” to this tabular data. In the time series data, the index becomes increasingly significant. Any data point within the DataFrame or series can be retrieved using the index, which functions like an address. This tutorial explains the method which is used for adding the “index” in “Pandas”. Let’s try some examples here and check how the indexes are added in “Pandas”.

Example 1:

We start with the first example of this tutorial. The software that we utilize in this tutorial is the “Sypder” software and it is best for performing the “Pandas” and “Python” codes. Here at the code, we start doing this code by importing the “pandas as pd”. We utilize it here so we can easily get the “pandas” methods by putting the “pd” instead of “pandas”. We create a DataFrame “Report”. It contains five columns and four rows. We add the information about the students. Then, we put “11”, “Smith”, “72”, “67”, and “77” in the first row. The second row contains “12”, “Lini”, “78”, “69”, and “74”.

Now, comes the third row and we have “13”, “Michael”, “74”, “56”, and “55”. In the last row, we add “14”, “Allies”,”54”, “76”, and “67”. After this, we put some column names which are “Roll_No”, “Name”, “English”, “Mathematics”, and “Science”. Then, we utilize the “print” to show this “Report” on the screen. Now, we set the index by using the “set_index()” method. We set the “Roll_No” as the “index”. And then, print this again.

Press “Shift+Enter”. The following output will render on the console screen of the Sypder. The first tabular data have the index which is automatically added but we add the index and display it below. In the second tabular data, the “Roll_No” is the index.

Example 2:

In the previous code, we created only one index but now in this code, we select multiple columns as the index. We import the “pandas as pd” at the start of the code and we have a DataFrame with the name “Progress”. We add some data to it and creat five columns. We first put the data in rows and then add the names of the columns after adding the data in the rows. We also render this DataFrame.

Then, we use the “set_index” method in which we add two column names to set them as indexes. We select “Roll_No” and “Name” as the index. We put the square bracket in this “set_index()” method and add the names of columns in this square bracket separating them by a comma “,”. Then, this “Progress” DataFrame is also printed in the following with two indexes.

In the second DataFrame, we have two indexes which are “Roll_No” and “Name”. It means that we can easily add multiple indexes in “pandas” by using the “set_index” method.

Example 3:

We “import” the “panda” as the “pd” and then we move to create and initialize a nested list with the name “students”. We have “James”, “35”, “Sodan”, “Australia”, “85.96”, and “100” in the first list. And then we put “Riti”, “30”, “Delhi”, “India”, “95.20”, and “100” as the second list. The third list contains “Vansh”, “39”, “Delhi”, “India”, “85.25”, and “100”. And the fourth list has “William”, “32”, “Tokyo”, “Japan”, “74.21” and “100”. In the fifth list, we insert “Maychan”, “34”, “New York”, “US”, “99.63”, and “100”.

Now, we move to create the last list and add some data in this list. We add “Mike”, “33”, “Las Vegas”, “US”, “47.28”, and “100”. Now, we alter this nested list into the DataFrame and set the DataFrame name’s as “data”. We also add some column names because, in DataFrame, the data will render in the table form. We add some column names which are “S_Name”, “S_Age”, “Current_City”, “Current_Loc”, “Obt_Marks”, and “Total_Marks”. These names appears on top as the name of the column. We also set some index values here which are “a”, “b”, “c”, “d”, “e”, and “f” and print this complete DataFrame “data”.

We set the float column “Obt_Marks” below and set it as the index by simply putting it in the “set_index ()” method. It changes the previous index and set the “Obt_Marks” as the index. This also renders on the console screen as we utilize the “print()” function.

The first column is the index. In the first tabular data, you can notice that the indexes are those values that we previously added. In the following illustration, it changes the index column and set the “Obt_Marks” as the index because we altered the previous index and set the new index in our code.

Example 4:

In the previous code, there was only one index created. However, in this code, many columns are used as the index. At the beginning of the code, we import the “pandas as pd”. We change the name of the nested list and set it as “studentData”. The DataFrame here is “data1”. We place three different column names in the “set_index()” method because we want to add three indexes here. In this code, we set the “S_Name”, “ID”, and “Current_City” as the index.

As you can see in the first tabular data, the index is the first column and the indexes are the alphabets that we added earlier. You can view the three indexes that are present in the second DataFrame and the three columns shifted to the left side of this tabular data.

Example 5:

We create the “dict” here as “employee_dict” and insert some data in it. In the name, we put “Joe”, “Nat”, and “Harry”. In the “Salary”, we add “2000”, “2100”, and “1900”. Then, we also add “Bonus” in which we insert the “5%”, “3%”, and “2%”. Then, we change this “dict” into the DataFrame. The “print” method is utilized for rendering. We add the indexes in such a way that the index is set and the column remains. We utilize the “set_index()” method in which we add the “Name” to set it as an index. We also put the “drop=False” so the name column appears as the index and as the column.

Note that two columns have the name “Name” in which the first “Name” is the index and the other “Name” is the column. We set the index and keep the column.

Conclusion

This tutorial is created to assist you in learning how to “add index” in “Pandas”. We run several examples using the “set_index” approach. Along with the code scripts, the outputs are rendered. Each code was thoroughly discussed here. I hope it will be simpler for you to know how to add an index and which method is utilized in “Pandas” to do so.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.