Python是進行數據分析的一種出色語言,主要是因為以數據為中心的python軟件包具有奇妙的生態係統。 Pandas是其中的一種,使導入和分析數據更加容易。
Pandas DatetimeIndex.to_frame()
函數使用包含索引的列創建一個DataFrame。默認情況下,DatetimeIndex對象的標簽用作新構造的Dataframe的索引。
用法: DatetimeIndex.to_frame(index=True)
參數:
index:將返回的DataFrame的索引設置為原始索引
返回:包含原始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)
輸出:
現在我們要從DatetimeIndex對象構造一個DataFrame。
# construct the DataFrame
didx.to_frame(index = False)
輸出:
從輸出中可以看到,該函數返回了一個由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)
輸出:
現在我們要從DatetimeIndex對象構造一個DataFrame。
# construct the DataFrame
didx.to_frame(index = True)
輸出:
從輸出中可以看到,該函數返回了一個由didx DatetimeIndex對象構造的DataFrame對象。
相關用法
- Python pandas.map()用法及代碼示例
- Python Pandas Series.max()用法及代碼示例
- Python Pandas Series.pow()用法及代碼示例
- Python Pandas dataframe.get()用法及代碼示例
- Python Pandas Series.div()用法及代碼示例
- Python Pandas Series.xs用法及代碼示例
- Python Pandas Series.min()用法及代碼示例
- Python Pandas PeriodIndex.day用法及代碼示例
- Python Pandas Index.min()用法及代碼示例
- Python Pandas Series.where用法及代碼示例
- Python Pandas Series.loc用法及代碼示例
- Python Pandas dataframe.eq()用法及代碼示例
- Python Pandas Series.sub()用法及代碼示例
- Python Pandas Series.add()用法及代碼示例
- Python Pandas Series.mean()用法及代碼示例
注:本文由純淨天空篩選整理自Shubham__Ranjan大神的英文原創作品 Python | Pandas DatetimeIndex.to_frame()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。