Python Pandas DatetimeIndex.to_frame()

Python Pandas DatetimeIndex.to_frame()

Python是一种进行数据分析的伟大语言,主要是因为以数据为中心的Python包的奇妙生态系统。Pandas就是这些包中的一个,它使导入和分析数据变得更加容易。

Pandas DatetimeIndex.to_frame()函数创建了一个DataFrame,其中有一列包含索引。默认情况下,DatetimeIndex对象的标签被用作新构建的Dataframe的索引。

语法: DatetimeIndex.to_frame(index=True)

参数 :
index : 将返回的DataFrame的索引设置为原始索引

返回:包含原始索引数据的数据框架。

示例#1:使用DatetimeIndex.to_frame()函数,从给定的DatetimeIndex对象中创建一个DataFrame对象。同时将索引值设置为False

# importing pandas as pd
import pandas as pd
  
# Create the DatetimeIndex
# Here 'S' represents secondly frequency 
didx = pd.DatetimeIndex(start ='2018-11-15 09:45:10', freq ='S', periods = 5)
  
# Print the DatetimeIndex
print(didx)
Python

输出 :
Python Pandas DatetimeIndex.to_frame()

现在我们要从DatetimeIndex对象中构造一个DataFrame。

# construct the DataFrame
didx.to_frame(index = False)
Python

输出 :
Python Pandas DatetimeIndex.to_frame()
正如我们在输出中看到的,该函数返回了一个由didx DatetimeIndex对象构建的DataFrame对象。

示例#2:使用DatetimeIndex.to_frame()函数从给定的DatetimeIndex对象创建一个DataFrame对象。

# importing pandas as pd
import pandas as pd
  
# Create the DatetimeIndex
# Here 'M' represents monthly frequency 
didx = pd.DatetimeIndex(start ='2015-03-02', freq ='M', periods = 5)
  
# Print the DatetimeIndex
print(didx)
Python

输出 :
Python Pandas DatetimeIndex.to_frame()

现在我们要从DatetimeIndex对象中构造一个DataFrame。

# construct the DataFrame
didx.to_frame(index = True)
Python

输出 :
Python Pandas DatetimeIndex.to_frame()
正如我们在输出中看到的,该函数返回了一个由didx DatetimeIndex对象构建的DataFrame对象。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

Pandas 日期时间

登录

注册