如何转换Pandas数据框架中某一列的索引

如何转换Pandas数据框架中某一列的索引

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

示例:

# 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

输出:

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

创建的数据框架

方法1:创建一个新的索引列

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

# 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

输出:

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

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

方法2:使用reset_index()方法和to_string()方法。

在这里,我们将使用reset_index()方法将索引转换为列,同时使用inplace参数来持续反映变化,我们将使用to_string()方法来隐藏默认显示的索引值,无论何时打印数据框架。

#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))

输出:

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

使用reset_index方法

在这里,我们对给定的数据框架应用了reset_index方法,并将其作为参数传递给to_string方法,使默认的索引值(等于0)。

方法3:使用multi_index

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

例子1:对于多索引到列

# 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

输出:

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

示例2:将多指标中的特定列作为数据框架的一个列。

# 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

输出:

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程