Python是进行数据分析的一种出色语言,主要是因为以数据为中心的python软件包具有奇妙的生态系统。 Pandas是其中的一种,使导入和分析数据更加容易。
Pandas Index.to_frame()
函数根据给定索引创建一个dataFrame,其中包含包含Index的列。默认情况下,原始索引将在新数据帧中重用。为了增强新创建的 DataFrame 的新索引,我们将函数的index参数设置为false。
用法: Index.to_frame(index=True)
参数:
index:将返回的DataFrame的索引设置为原始索引。
返回:包含原始Index数据的DataFrame。
范例1:采用Index.to_frame()
函数将索引转换为数据帧。
# importing pandas as pd
import pandas as pd
# Creating the index
idx = pd.Index(['Alice', 'Bob', 'Rachel', 'Tyler',
'Louis'], name ='Winners')
# Print the Index
idx
输出:
让我们将索引转换为 DataFrame 。
# convert the index into a dataframe
idx.to_frame()
输出:
该函数已将索引转换为数据帧。默认情况下,该函数已使用原始Index的值创建了 DataFrame 的索引。
范例2:采用Index.to_frame()
函数将索引转换为 DataFrame ,以便创建的 DataFrame 使用新的索引值。
# importing pandas as pd
import pandas as pd
# Creating the index
idx = pd.Index([22, 54, 85, 45, 69, 33])
# Print the Index
idx
输出:
让我们将索引转换为 DataFrame 。
# convert the index into a dataframe
idx.to_frame(index = False)
输出:
相关用法
- Python pandas.map()用法及代码示例
- Python Pandas Series.str.len()用法及代码示例
- Python Pandas.factorize()用法及代码示例
- Python Pandas TimedeltaIndex.name用法及代码示例
- Python Pandas dataframe.ne()用法及代码示例
- Python Pandas Series.between()用法及代码示例
- Python Pandas DataFrame.where()用法及代码示例
- Python Pandas Series.add()用法及代码示例
- Python Pandas.pivot_table()用法及代码示例
- Python Pandas Series.mod()用法及代码示例
- Python Pandas Dataframe.at[ ]用法及代码示例
- Python Pandas Dataframe.iat[ ]用法及代码示例
- Python Pandas.pivot()用法及代码示例
- Python Pandas dataframe.mul()用法及代码示例
- Python Pandas.melt()用法及代码示例
注:本文由纯净天空筛选整理自Shubham__Ranjan大神的英文原创作品 Python | Pandas Index.to_frame()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。