📜  Python|熊猫 Series.to_frame()

📅  最后修改于: 2022-05-13 01:55:40.149000             🧑  作者: Mango

Python|熊猫 Series.to_frame()

Pandas 系列是带有轴标签的一维 ndarray。标签不必是唯一的,但必须是可散列的类型。该对象支持基于整数和基于标签的索引,并提供了许多用于执行涉及索引的操作的方法。

Pandas Series.to_frame()函数用于将给定的系列对象转换为数据框。

示例 #1:使用Series.to_frame()函数将给定的系列对象转换为数据框。

# importing pandas as pd
import pandas as pd
  
# Creating the Series
sr = pd.Series(['New York', 'Chicago', 'Toronto', 'Lisbon', 'Rio', 'Moscow'])
  
# Create the Datetime Index
didx = pd.DatetimeIndex(start ='2014-08-01 10:00', freq ='W', 
                     periods = 6, tz = 'Europe/Berlin') 
  
# set the index
sr.index = didx
  
# Print the series
print(sr)

输出 :

现在我们将使用Series.to_frame()函数将给定的系列对象转换为数据框。

# convert to dataframe
sr.to_frame()

输出 :

正如我们在输出中看到的, Series.to_frame()函数已成功地将给定的系列对象转换为数据帧。

示例 #2:使用Series.to_frame()函数将给定的系列对象转换为数据框。

# importing pandas as pd
import pandas as pd
  
# Creating the Series
sr = pd.Series([19.5, 16.8, 22.78, 20.124, 18.1002])
  
# Print the series
print(sr)

输出 :

现在我们将使用Series.to_frame()函数将给定的系列对象转换为数据框。

# convert to dataframe
sr.to_frame()

输出 :

正如我们在输出中看到的, Series.to_frame()函数已成功地将给定的系列对象转换为数据帧。