大家好!在这个Python教程中,我们将讨论如何将一个DataFrame索引转换为列。我们还将看到如何将一个多索引的DataFrame的多级索引转换为其多列。那么让我们开始吧。
什么是pandas DataFrame中的索引?
Pandas是一个强大的Python库,被广泛用于数据分析。它为我们提供了一个叫做DataFrame 的数据结构,它以行和列的形式存储数据,每一行都有一个唯一的索引值。一个pandas DataFrame对象可以有多于一层的索引,在这种情况下,它被称为多索引DataFrame。
每当我们创建一个panda DataFrame对象时,默认情况下,一个从0到行数-1的索引值会按顺序分配给DataFrame的每一行。尽管我们也可以使用pandas中的DataFrame.set_index() 函数手动设置pandas DataFrame对象的每一行的索引值。
我们可以使用以下两种方法将pandas DataFrame对象的一个或多个级别的索引转换为它的列。为了演示将DataFrame索引转化为列的过程,我们首先创建一个pandas DataFrame对象。
将索引转换为潘达斯数据框架的列的方法
# Import pandas Python module
import pandas as pd
# Create a pandas DataFrame object
df = pd.DataFrame({'Dept': ['ECE', 'ICE', 'IT', 'CSE', 'CHE'],
'GPA': [8.15, 9.03, 7.85, 8.55, 9.45],
'Name': ['Kirti', 'Sarthak', 'Anubhav', 'Ranjan', 'Kartik'],
'RegNo': [111, 112, 113, 114, 115]})
# Set 'RegNo' as index of the pandas DataFrame
df.set_index('RegNo', inplace=True)
# Print the created pandas DataFrame object
print('Sample pandas DataFrame:\n')
print(df)
输出
Sample pandas DataFrame:
Dept GPA Name
RegNo
111 ECE 8.15 Kirti
112 ICE 9.03 Sarthak
113 IT 7.85 Anubhav
114 CSE 8.55 Ranjan
115 CHE 9.45 Kartik
方法1:创建一个新的DataFrame列并传递索引
这是最简单的转换DataFrame索引为列的方法。在这个方法中,我们只是在DataFrame中创建一个新的列,并使用pandas DataFrame类的DataFrame.index 方法将索引传递给它。让我们看看实现这个方法的Python代码。
# Method 1
# Convert the index of the sample DataFrame into column
# Using the new column method
df['Roll'] = df.index
# Print the modified pandas DataFrame
print('Modified pandas DataFrame:\n')
print(df)
输出
Modified pandas DataFrame:
Dept GPA Name Roll
RegNo
111 ECE 8.15 Kirti 111
112 ICE 9.03 Sarthak 112
113 IT 7.85 Anubhav 113
114 CSE 8.55 Ranjan 114
115 CHE 9.45 Kartik 115
方法2:在pandas中使用DataFrame.reset_index()函数
这是一种广泛使用的方法,可以将DataFrame的一个或多个级别的索引变成一个或多个列。在这个方法中,我们将使用pandas DataFrame类的DataFrame.reset_index() 函数。让我们编写Python代码来实现这个方法。
# Method 2
# Convert the index of the sample DataFrame into column
# Using the DataFrame.reset_index() function
df.reset_index(inplace=True)
# Print the modified pandas DataFrame
print('Modified pandas DataFrame:\n')
print(df)
输出
Modified pandas DataFrame:
RegNo Dept GPA Name
0 111 ECE 8.15 Kirti
1 112 ICE 9.03 Sarthak
2 113 IT 7.85 Anubhav
3 114 CSE 8.55 Ranjan
4 115 CHE 9.45 Kartik
将多索引数据框架的一个或多个层次转换为列
DataFrame.set_index() 让我们首先将上述样本数据框架转换为多索引数据框架,通过使用RegNo 和Name 函数设置为样本数据框架的多级索引。
# Convert the sample DataFrame into MultiIndex DataFrame
# By setting the 'RegNo' and 'Name' as Multi-level index
df.set_index(['RegNo', 'Name'], inplace=True)
# Print the modified pandas DataFrame
print('Modified Sample pandas DataFrame:\n')
print(df)
输出
Modified Sample pandas DataFrame:
Dept GPA
RegNo Name
111 Kirti ECE 8.15
112 Sarthak ICE 9.03
113 Anubhav IT 7.85
114 Ranjan CSE 8.55
115 Kartik CHE 9.45
现在让我们编写Python代码,使用DataFrame.reset_index() 函数将样本多索引数据框架中只有一个索引级别的数据转换为一个列。
# Convert one level of the MultiIndex DataFrame into column
# Using the DataFrame.reset_index() function
df.reset_index(level='Name', inplace=True)
# Print the modified pandas DataFrame
print('Modified pandas DataFrame:\n')
print(df)
输出
Modified pandas DataFrame:
Name Dept GPA
RegNo
111 Kirti ECE 8.15
112 Sarthak ICE 9.03
113 Anubhav IT 7.85
114 Ranjan CSE 8.55
115 Kartik CHE 9.45
求和
在本教程中,我们已经学会了如何将pandas DataFrame的索引转换为其列。我们还学习了如何将多索引数据框架的一个或多个级别的索引转换为其列。希望你已经理解了上面讨论的内容,并准备好用你自己的pandas DataFrame进行实验。谢谢你的阅读!请继续关注我们,了解更多与Python编程相关的精彩学习内容。