Setting and Resetting the Index#

Creating a pandas DataFrame and setting and resetting the index.

Importing libraries and packages#

1# Mathematical operations and data manipulation
2import numpy as np
3import pandas as pd

Creating dataset#

1matrix_data = np.matrix("22,66,140;42,70,148;30,62,125;35,68,160;25,62,152")
2row_labels = ["A", "B", "C", "D", "E"]
3column_headings = ["Age", "Height", "Weight"]
4
5df = pd.DataFrame(data=matrix_data, index=row_labels, columns=column_headings)
6df
Age Height Weight
A 22 66 140
B 42 70 148
C 30 62 125
D 35 68 160
E 25 62 152

Wrangling#

1df.reset_index()
index Age Height Weight
0 A 22 66 140
1 B 42 70 148
2 C 30 62 125
3 D 35 68 160
4 E 25 62 152
1df.reset_index(drop=True)
Age Height Weight
0 22 66 140
1 42 70 148
2 30 62 125
3 35 68 160
4 25 62 152
1df["Profession"] = "Student Teacher Engineer Doctor Nurse".split()
2df
Age Height Weight Profession
A 22 66 140 Student
B 42 70 148 Teacher
C 30 62 125 Engineer
D 35 68 160 Doctor
E 25 62 152 Nurse
1df.set_index("Profession")
Age Height Weight
Profession
Student 22 66 140
Teacher 42 70 148
Engineer 30 62 125
Doctor 35 68 160
Nurse 25 62 152