📌  相关文章
📜  如何转换 Pandas 数据框列中的索引?

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

如何转换 Pandas 数据框列中的索引?

数据帧中的每一行(即 level=0)都有一个索引值,即从 0 到 n-1 索引位置的值,并且有很多方法可以将这些索引值转换为 pandas 数据帧中的列。首先,让我们创建一个 Pandas 数据框。在这里,我们将创建一个关于学生在特定科目中的分数的 Pandas 数据框,其中列有卷号、姓名、分数、成绩和科目。

例子:

Python3
# importing the pandas library as pd
import pandas as pd    
 
 
# Creating the dataframe Ab
AB = pd.DataFrame({'Roll Number': ['9917102206', '9917102250',
                                   '9917102203', '9917102204',
                                   '9917102231'],
                   'Name': ['TANYA', 'PREETIKA', 'KUSHAGRA',
                            'PRAKHAR','ASHISH'],
                   'Score': [99, 98, 50, 45,97],
                   'Grade': ['A+', 'A+', 'C+', 'C','A'],
                   'Subject': ['Operating Systems', 'Operating Systems',
                               'Operating Systems', 'Operating Systems',
                               'Operating Systems']})
   
# Printing the dataframe
AB


Python3
# importing the pandas library as pd
import pandas as pd    
 
 
# Creating the dataframe Ab
AB = pd.DataFrame({'Roll Number': ['9917102206', '9917102250',
                                   '9917102203', '9917102204',
                                   '9917102231'],
                   'Name': ['TANYA', 'PREETIKA', 'KUSHAGRA',
                            'PRAKHAR','ASHISH'],
                   'Score': [99, 98, 50, 45,97],
                   'Grade': ['A+', 'A+', 'C+', 'C','A'],
                   'Subject': ['Operating Systems', 'Operating Systems',
                               'Operating Systems', 'Operating Systems',
                               'Operating Systems']})
   
     
# Adding a new index column to existing
# data frame and passing index value  
AB['index'] = AB.index
 
# Printing the dataframe
AB


Python3
#CREATING A DATAFRAME FOR STUDENTS PORTFOLIO
 
# importing the pandas library as pd
import pandas as pd    
# Creating the dataframe Ab
AB = pd.DataFrame({'Roll Number': ['9917102206', '9917102250', '9917102203', '9917102204','9917102231'],
                   'Name': ['TANYA', 'PREETIKA', 'KUSHAGRA', 'PRAKHAR','ASHISH'],
                   'Score': [99, 98, 50, 45,97],
                   'Grade': ['A+', 'A+', 'C+', 'C','A'],
                   'Subject': ['Operating Systems', 'Operating Systems', 'Operating Systems', 'Operating Systems','Operating Systems']})
   
     
# importing the pandas library as pd
import pandas as pd    
 
 
# Creating the dataframe Ab
AB = pd.DataFrame({'Roll Number': ['9917102206', '9917102250',
                                   '9917102203', '9917102204',
                                   '9917102231'],
                   'Name': ['TANYA', 'PREETIKA', 'KUSHAGRA',
                            'PRAKHAR','ASHISH'],
                   'Score': [99, 98, 50, 45,97],
                   'Grade': ['A+', 'A+', 'C+', 'C','A'],
                   'Subject': ['Operating Systems', 'Operating Systems',
                               'Operating Systems', 'Operating Systems',
                               'Operating Systems']})
   
     
# USING RESET_INDEX METHOD
# Adding a new index column to AB dataframe
AB.reset_index(level=0, inplace=True)
 
# HIDING THE DEFAULT INDEX VALUES AND
# PRINTING DATAFRAME
print( AB.to_string(index=False))


Python3
# importing the pandas library as pd
import pandas as pd    
 
# ADDING MULTI INDEX TO DATA FRAME
new_index = pd.MultiIndex.from_tuples([('E4','ECE'),
                                       ('E5','ECE'),
                                       ('E6','ECE'),
                                       ('E7','ECE'),
                                       ('E8','ECE')],
                                       names=['BATCH','BRANCH'])
# Creating the dataframe AB
data =({'Roll Number': ['9917102206', '9917102250',
                        '9917102203', '9917102204',
                        '9917102231'],
                   'Name': ['TANYA', 'PREETIKA', 'KUSHAGRA',
                            'PRAKHAR','ASHISH'],
                   'Score': [99, 98, 50, 45,97],
                   'Grade': ['A+', 'A+', 'C+', 'C','A'],
                   'Subject': ['Operating Systems', 'Operating Systems',
                               'Operating Systems', 'Operating Systems',
                               'Operating Systems']})
   
# COMBING DATA FRAME AND MULTI INDEX
# VALUES AND FORMING DATA FRAME
AB = pd.DataFrame(data, columns = ['Roll Number','Name','Score','Grade','Subject'],
                  index=new_index)
 
# MAKING MULTI INDEX NOW A PART OF COLUMN
# OF DATAFRAME
AB.reset_index(inplace=True)
 
AB


Python3
# importing the pandas library as pd
import pandas as pd    
 
# ADDING MULTI INDEX TO DATA FRAME
new_index = pd.MultiIndex.from_tuples([('E4','ECE'),
                                       ('E5','ECE'),
                                       ('E6','ECE'),
                                       ('E7','ECE'),
                                       ('E8','ECE')],
                                       names=['BATCH','BRANCH'])
# Creating the dataframe AB
data =({'Roll Number': ['9917102206', '9917102250',
                        '9917102203', '9917102204',
                        '9917102231'],
                   'Name': ['TANYA', 'PREETIKA', 'KUSHAGRA',
                            'PRAKHAR','ASHISH'],
                   'Score': [99, 98, 50, 45,97],
                   'Grade': ['A+', 'A+', 'C+', 'C','A'],
                   'Subject': ['Operating Systems', 'Operating Systems',
                               'Operating Systems', 'Operating Systems',
                               'Operating Systems']})
   
# COMBING DATA FRAME AND MULTI INDEX
# VALUES AND FORMING DATA FRAME
AB = pd.DataFrame(data, columns = ['Roll Number','Name','Score','Grade','Subject'],
                  index=new_index)
 
# MAKING SPECIFIC COLUMN OF MULTI INDEX
# NOW A PART OF COLUMN OF DATAFRAME
AB.reset_index(inplace=True,level=['BATCH'])
 
# BATCH INDEX IS NOW A COLUMN OF DATAFRAME
AB


输出:

创建的数据框

方法一:新建索引列

在这里,我们将学习在现有数据框中创建一个新列作为索引,并将每一行的索引值(级别 = 0)添加到该列。

Python3

# importing the pandas library as pd
import pandas as pd    
 
 
# Creating the dataframe Ab
AB = pd.DataFrame({'Roll Number': ['9917102206', '9917102250',
                                   '9917102203', '9917102204',
                                   '9917102231'],
                   'Name': ['TANYA', 'PREETIKA', 'KUSHAGRA',
                            'PRAKHAR','ASHISH'],
                   'Score': [99, 98, 50, 45,97],
                   'Grade': ['A+', 'A+', 'C+', 'C','A'],
                   'Subject': ['Operating Systems', 'Operating Systems',
                               'Operating Systems', 'Operating Systems',
                               'Operating Systems']})
   
     
# Adding a new index column to existing
# data frame and passing index value  
AB['index'] = AB.index
 
# Printing the dataframe
AB

输出:

在这里,我们使用数据帧中每一行的索引值作为参数值,将新列“索引”添加到“AB”数据帧,并将索引转换为列。

方法二:使用reset_index()方法和to_string()方法

在这里,我们将使用 reset_index() 方法将索引转换为列以及 inplace 参数以连续反映更改,并且我们将使用 to_string() 方法在打印数据帧时隐藏默认显示的索引值。

Python3

#CREATING A DATAFRAME FOR STUDENTS PORTFOLIO
 
# importing the pandas library as pd
import pandas as pd    
# Creating the dataframe Ab
AB = pd.DataFrame({'Roll Number': ['9917102206', '9917102250', '9917102203', '9917102204','9917102231'],
                   'Name': ['TANYA', 'PREETIKA', 'KUSHAGRA', 'PRAKHAR','ASHISH'],
                   'Score': [99, 98, 50, 45,97],
                   'Grade': ['A+', 'A+', 'C+', 'C','A'],
                   'Subject': ['Operating Systems', 'Operating Systems', 'Operating Systems', 'Operating Systems','Operating Systems']})
   
     
# importing the pandas library as pd
import pandas as pd    
 
 
# Creating the dataframe Ab
AB = pd.DataFrame({'Roll Number': ['9917102206', '9917102250',
                                   '9917102203', '9917102204',
                                   '9917102231'],
                   'Name': ['TANYA', 'PREETIKA', 'KUSHAGRA',
                            'PRAKHAR','ASHISH'],
                   'Score': [99, 98, 50, 45,97],
                   'Grade': ['A+', 'A+', 'C+', 'C','A'],
                   'Subject': ['Operating Systems', 'Operating Systems',
                               'Operating Systems', 'Operating Systems',
                               'Operating Systems']})
   
     
# USING RESET_INDEX METHOD
# Adding a new index column to AB dataframe
AB.reset_index(level=0, inplace=True)
 
# HIDING THE DEFAULT INDEX VALUES AND
# PRINTING DATAFRAME
print( AB.to_string(index=False))

输出:

使用 RESET_INDEX 方法

在这里,我们将 reset_index 方法应用于给定的数据帧,并通过将其作为参数传递给 to_string 方法来设置默认索引值(等于 0)。

方法3:使用multi_index

在这里,我们将学习从具有多索引的数据框中创建列。

示例 1:对于列的多索引

Python3

# importing the pandas library as pd
import pandas as pd    
 
# ADDING MULTI INDEX TO DATA FRAME
new_index = pd.MultiIndex.from_tuples([('E4','ECE'),
                                       ('E5','ECE'),
                                       ('E6','ECE'),
                                       ('E7','ECE'),
                                       ('E8','ECE')],
                                       names=['BATCH','BRANCH'])
# Creating the dataframe AB
data =({'Roll Number': ['9917102206', '9917102250',
                        '9917102203', '9917102204',
                        '9917102231'],
                   'Name': ['TANYA', 'PREETIKA', 'KUSHAGRA',
                            'PRAKHAR','ASHISH'],
                   'Score': [99, 98, 50, 45,97],
                   'Grade': ['A+', 'A+', 'C+', 'C','A'],
                   'Subject': ['Operating Systems', 'Operating Systems',
                               'Operating Systems', 'Operating Systems',
                               'Operating Systems']})
   
# COMBING DATA FRAME AND MULTI INDEX
# VALUES AND FORMING DATA FRAME
AB = pd.DataFrame(data, columns = ['Roll Number','Name','Score','Grade','Subject'],
                  index=new_index)
 
# MAKING MULTI INDEX NOW A PART OF COLUMN
# OF DATAFRAME
AB.reset_index(inplace=True)
 
AB

输出:

示例 2:用于将多索引中的特定列作为数据框的列

Python3

# importing the pandas library as pd
import pandas as pd    
 
# ADDING MULTI INDEX TO DATA FRAME
new_index = pd.MultiIndex.from_tuples([('E4','ECE'),
                                       ('E5','ECE'),
                                       ('E6','ECE'),
                                       ('E7','ECE'),
                                       ('E8','ECE')],
                                       names=['BATCH','BRANCH'])
# Creating the dataframe AB
data =({'Roll Number': ['9917102206', '9917102250',
                        '9917102203', '9917102204',
                        '9917102231'],
                   'Name': ['TANYA', 'PREETIKA', 'KUSHAGRA',
                            'PRAKHAR','ASHISH'],
                   'Score': [99, 98, 50, 45,97],
                   'Grade': ['A+', 'A+', 'C+', 'C','A'],
                   'Subject': ['Operating Systems', 'Operating Systems',
                               'Operating Systems', 'Operating Systems',
                               'Operating Systems']})
   
# COMBING DATA FRAME AND MULTI INDEX
# VALUES AND FORMING DATA FRAME
AB = pd.DataFrame(data, columns = ['Roll Number','Name','Score','Grade','Subject'],
                  index=new_index)
 
# MAKING SPECIFIC COLUMN OF MULTI INDEX
# NOW A PART OF COLUMN OF DATAFRAME
AB.reset_index(inplace=True,level=['BATCH'])
 
# BATCH INDEX IS NOW A COLUMN OF DATAFRAME
AB

输出: