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

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

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

Pandas Series.transpose() function return the transpose, which is by definition self.

Syntax: Series.transpose(*args, **kwargs)

Parameter : None

Returns : self

Example #1: Use Series.transpose() function to find the transpose of the given Series object.

# 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.transpose() function to find the transpose of the given series object.

# find the transpose
sr.transpose()

Выход :

As we can see in the output, the Series.transpose() function has returned the same object as the transpose of the given series object, which is by definition self.
 
Example #2: Use Dataframe.transpose() function to find the transpose of the given Dataframe.

# importing pandas as pd
import pandas as pd
  
# Creating the Dataframe
df = pd.DataFrame({"Date":["10/2/2011", "11/2/2011", "12/2/2011", "13/2/2011"],
                    "Event":["Music", "Poetry", "Theatre", "Comedy"],
                    "Cost":[10000, 5000, 15000, 2000]})
  
# Print the dataframe
print(df)

Выход :

Now we will use Dataframe.transpose() function to find the transpose of the given Dataframe.

# find the transpose
df.transpose()

Выход :

As we can see in the output, the Dataframe.transpose() function has successfully returned the transpose of the given Dataframe object.

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

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