Informatics Practices · Ch 3 — Data Handling using Pandas – II
Altering the Index
Altering the Index
Altering the Index
The index of a DataFrame is the set of row labels used to access and retrieve data quickly. By default, Pandas assigns a numeric index starting from 0, as shown in the sample DataFrame of student marks across different Unit Tests (UT). In that table, the first column (0, 1, 2, …) is the default index.
However, the default index may not always suit your needs. You might want to use an existing column (like student name or UT number) as the index, or you may need to clean up a non‑continuous index after slicing data. Pandas provides three key functions to alter the index: reset_index(), drop(), and set_index().
The Problem of Non‑Continuous Index After Slicing
When you filter a DataFrame (for example, selecting only rows where UT == 1), the resulting slice retains the original index values. These values are no longer consecutive — they are the original row numbers from the parent DataFrame. For instance, after selecting Unit Test 1 marks, the index might be 0, 3, 6, 9 instead of 0, 1, 2, 3. This non‑continuous index can be inconvenient for further operations.
Using reset_index() to Create a New Continuous Index
The reset_index() function resets the index to a default sequence of integers (0, 1, 2, …). By default, it does not modify the original DataFrame — it returns a new DataFrame. To change the original DataFrame itself, use the inplace=True parameter.
When you call reset_index(inplace=True), a new column named 'index' is added to the DataFrame. This column holds the old index values, so you can still refer to them if needed. The new continuous index becomes the row label.
After reset_index(), the old index is preserved as a separate column called 'index'. You can keep it or drop it.
Dropping the Old Index Column with drop()
If you do not need the old index values, you can remove the 'index' column using the drop() function. Specify columns=['index'] and set inplace=True to modify the DataFrame directly.
dfUT1.drop(columns=['index'], inplace=True)
After this, the DataFrame has only the new continuous index (0, 1, 2, …) and no trace of the original index.
Changing the Index to Another Column with set_index()
You can make any existing column the new row index using set_index(). For example, to set the 'Name' column as the index:
dfUT1.set_index('Name', inplace=True)
Now the row labels become the student names (Raman, Zuhaire, Ashravy, Mishti), and the 'Name' column disappears from the data area — it becomes the index. The original numeric index is replaced.
set_index() does not keep the old index as a column. If you need the old index later, reset it first or store it separately.
Reverting to the Previous Index
To revert back to the default numeric index after using set_index(), call reset_index() again. This time, you can specify the name of the column that is currently the index (e.g., 'Name') to move it back to a regular column:
dfUT1.reset_index('Name', inplace=True)
This restores the default integer index and brings the 'Name' column back into the DataFrame as a regular column.
Summary of Functions …
| Index | Name | UT | Maths | Science | S.St | Hindi | Eng |
|---|---|---|---|---|---|---|---|
| 0 | Raman | 1 | 22 | 21 | 18 | 20 | 21 |
| 1 | Raman | 2 | 21 | 20 | 17 | 22 | 24 |
| 2 | Raman | 3 | 14 | 19 | 15 | 24 | 23 |
| 3 | Zuhaire | 1 | 20 | 17 | 22 | 24 | 19 |
| 4 | Zuhaire | 2 | 23 | 15 | 21 | 25 | 15 |
| 5 | Zuhaire | 3 | 22 | 18 | 19 | 23 | 13 |
| 6 | Ashravy | 1 | 23 | 19 | 20 | 15 | 22 |
| 7 | Ashravy | 2 | 24 | 22 | 24 | 17 | 21 |
| 8 | Ashravy | 3 | 12 | 25 | 19 | 21 | 23 |
| 9 | Mishti | 1 | 15 | 22 | 25 | 22 | 22 |
| 10 | Mishti | 2 | 18 | 21 | 25 | 24 | 23 |
| 11 | Mishti | 3 | 17 | 18 | 20 | 25 | 20 |
| Index | Name | UT | Maths | Science | S.St | Hindi | Eng |
|---|---|---|---|---|---|---|---|
| 0 | Raman | 1 | 22 | 21 | 18 | 20 | 21 |
| 3 | Zuhaire | 1 | 20 | 17 | 22 | 24 | 19 |
| 6 | Ashravy | 1 | 23 | 19 | 20 | 15 | 22 |
| index | Name | UT | Maths | Science | S.St | Hindi | Eng | |
|---|---|---|---|---|---|---|---|---|
| 0 | 0 | Raman | 1 | 22 | 21 | 18 | 20 | 21 |
| 1 | 3 | Zuhaire | 1 | 20 | 17 | 22 | 24 | 19 |
| 2 | 6 | Ashravy | 1 | 23 | 19 | 20 | 15 | 22 |
| 3 | 9 | Mishti | 1 | 15 | 22 | 25 | 22 | 22 |
| Index | Name | UT | Maths | Science | S.St | Hindi | Eng |
|---|---|---|---|---|---|---|---|
| 0 | Raman | 1 | 22 | 21 | 18 | 20 | 21 |
| 1 | Zuhaire | 1 | 20 | 17 | 22 | 24 | 19 |
| 2 | Ashravy | 1 | 23 | 19 | 20 | 15 | 22 |
| Name | UT | Maths | Science | S.St | Hindi | Eng |
|---|---|---|---|---|---|---|
| Raman | 1 | 22 | 21 | 18 | 20 | 21 |
| Zuhaire | 1 | 20 | 17 | 22 | 24 | 19 |
| Ashravy | 1 | 23 | 19 | 20 | 15 | 22 |