📜  如何更改 Pandas 中的索引值?

📅  最后修改于: 2022-05-13 01:54:30.410000             🧑  作者: Mango

如何更改 Pandas 中的索引值?

索引用于唯一标识Pandas DataFrame中的一行。它只不过是一行的标签。如果我们在创建时没有为 DataFrame 指定索引值,那么它将采用默认值,即从 0 到 n-1 的数字,其中 n 表示行数。

让我们创建一个数据框

示例

Python3
# import necessary packages
import pandas as pd
 
# create a dataframe
Students = pd.DataFrame({'Admission_id': ['AB101', 'AB102', 'AB103',
                                          'AB104', 'AB105'],
                         'Student_id': ['21GFG1', '21GFG2', '21GFG3',
                                        '21GFG4', '21GFG5'],
                         'Student_Name': ['Akhil', 'Mahesh Babu', 'Warner',
                                          'Virat', 'ABD'],
                         'Height': [5.9, 6.2, 5.6, 5.8, 5.10]})
# display dataframe
Students


Python3
# import necessary packages
import pandas as pd
 
# create a dataframe
Students = pd.DataFrame({'Admission_id': ['AB101', 'AB102', 'AB103',
                                          'AB104', 'AB105'],
                         'Student_id': ['21GFG1', '21GFG2', '21GFG3',
                                        '21GFG4', '21GFG5'],
                         'Student_Name': ['Akhil', 'Mahesh Babu', 'Warner',
                                          'Virat', 'ABD'],
                         'Height': [5.9, 6.2, 5.6, 5.8, 5.10]})
 
# setting admission id as index but temporarily
Students.set_index("Admission_id")


Python3
# import necessary packages
import pandas as pd
 
# create a dataframe
Students = pd.DataFrame({'Admission_id': ['AB101', 'AB102', 'AB103',
                                          'AB104', 'AB105'],
                         'Student_id': ['21GFG1', '21GFG2', '21GFG3',
                                        '21GFG4', '21GFG5'],
                         'Student_Name': ['Akhil', 'Mahesh Babu', 'Warner',
                                          'Virat', 'ABD'],
                         'Height': [5.9, 6.2, 5.6, 5.8, 5.10]})
 
# setting student id as index but permanently
Students.set_index("Student_id", inplace=True)
 
# display dataframe
Students


Python3
# import necessary packages
import pandas as pd
 
# create a dataframe
Students = pd.DataFrame({'Admission_id': ['AB101', 'AB102', 'AB103',
                                          'AB104', 'AB105'],
                         'Student_id': ['21GFG1', '21GFG2', '21GFG3',
                                        '21GFG4', '21GFG5'],
                         'Student_Name': ['Akhil', 'Mahesh Babu', 'Warner',
                                          'Virat', 'ABD'],
                         'Height': [5.9, 6.2, 5.6, 5.8, 5.10]})
 
# setting student id as index but permanently
Students.set_index("Student_id", inplace=True)
 
# display dataframe with required columns
Students[["Student_Name", "Height"]]


输出:

方法 1:使用 set_index()

要更改索引值,我们需要使用 pandas 中可用的set_index方法,允许指定索引。

句法

在哪里,

  • inplace 参数接受 True 或 False,它指定索引的更改是永久的还是临时的。
  • True 表示更改是永久的。
  • False 表示更改是临时的。

示例 1:

通过指定inplace=False (或)临时更改索引,我们可以在不指定 inplace 参数的情况下进行更改,因为默认情况下 inplace 值为 false。

Python3

# import necessary packages
import pandas as pd
 
# create a dataframe
Students = pd.DataFrame({'Admission_id': ['AB101', 'AB102', 'AB103',
                                          'AB104', 'AB105'],
                         'Student_id': ['21GFG1', '21GFG2', '21GFG3',
                                        '21GFG4', '21GFG5'],
                         'Student_Name': ['Akhil', 'Mahesh Babu', 'Warner',
                                          'Virat', 'ABD'],
                         'Height': [5.9, 6.2, 5.6, 5.8, 5.10]})
 
# setting admission id as index but temporarily
Students.set_index("Admission_id")

输出:

但是当我们在 DataFrame 中显示数据时,它仍然和以前一样,因为执行的操作没有保存,因为它是临时操作。

说明– 由于我们没有set_index方法中指定inplace参数,默认情况下它被视为 false 并被视为临时操作。

示例 2:

通过在set_index方法中指定 inplace =True来永久更改索引。

示例

Python3

# import necessary packages
import pandas as pd
 
# create a dataframe
Students = pd.DataFrame({'Admission_id': ['AB101', 'AB102', 'AB103',
                                          'AB104', 'AB105'],
                         'Student_id': ['21GFG1', '21GFG2', '21GFG3',
                                        '21GFG4', '21GFG5'],
                         'Student_Name': ['Akhil', 'Mahesh Babu', 'Warner',
                                          'Virat', 'ABD'],
                         'Height': [5.9, 6.2, 5.6, 5.8, 5.10]})
 
# setting student id as index but permanently
Students.set_index("Student_id", inplace=True)
 
# display dataframe
Students

输出:

示例 3:

当我们只想检索特定列而不是所有列时,请遵循以下代码

Python3

# import necessary packages
import pandas as pd
 
# create a dataframe
Students = pd.DataFrame({'Admission_id': ['AB101', 'AB102', 'AB103',
                                          'AB104', 'AB105'],
                         'Student_id': ['21GFG1', '21GFG2', '21GFG3',
                                        '21GFG4', '21GFG5'],
                         'Student_Name': ['Akhil', 'Mahesh Babu', 'Warner',
                                          'Virat', 'ABD'],
                         'Height': [5.9, 6.2, 5.6, 5.8, 5.10]})
 
# setting student id as index but permanently
Students.set_index("Student_id", inplace=True)
 
# display dataframe with required columns
Students[["Student_Name", "Height"]]

输出