Python | Панды Series.to_frame ()

Опубликовано: 28 Марта, 2022

Серия Pandas - это одномерный массив ndarray с метками осей. Этикетки не обязательно должны быть уникальными, но должны быть хешируемого типа. Объект поддерживает индексирование как на основе целых чисел, так и на основе меток и предоставляет множество методов для выполнения операций, связанных с индексом.

Pandas Series.to_frame() function is used to convert the given series object to a dataframe.

Syntax: Series.to_frame(name=None)

Parameter :
name : The passed name should substitute for the series name (if it has one).

Returns : data_frame : DataFrame

Example #1: Use Series.to_frame() function to convert the given series object to a dataframe.

# 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)

Выход :

Now we will use Series.to_frame() function to convert the given series object to a dataframe.

# convert to dataframe
sr.to_frame()

Выход :

As we can see in the output, the Series.to_frame() function has successfully converted the given series object to a dataframe.

Example #2: Use Series.to_frame() function to convert the given series object to a dataframe.

Выход :

Now we will use Series.to_frame() function to convert the given series object to a dataframe.

# convert to dataframe
sr.to_frame()

Выход :

As we can see in the output, the Series.to_frame() function has successfully converted the given series object to a dataframe.

Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.

Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.