+

Python | Pandas Series.transpose ()

Series.transpose() Pandas Series.transpose() returns transpose, which is self .

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

Parameter: None

Returns: self

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

# import pandas as pd

import pandas as pd

 
# Create Series

sr = pd .Series ([ `New York` , `Chicago` , ` Toronto` , `Lisbon` , ` Rio` , `Moscow` ])

 
# Create date and time index

didx = pd.DatetimeIndex (start = ` 2014-08-01 10:00` , freq = `W`

  periods = 6 , tz = ` Europe / Berlin`

 
# set index

sr.index = didx

 
# Print series

print (sr)

Output:

We will now use Series.transpose () to find the transposition of this series object.

# find transposition
sr.transpose ()

Output:

As we can see in the output, Series.transpose () returned the same object as the transposition of this series object, which is self by definition.

Example # 2: Use Dataframe.transpose () to find the transposition of a given Dataframe.

# import pandas as pd

import pandas as pd

 
# Create data frame

df = pd.DataFrame ({ ` Date` : [ `10/2 / 2011` , `11/2 / 2011` , ` 12/2 / 2011` , `13 / 2 / 2011` ],

`Event` : [ ` Music` , `Poetry` , ` Theater` , `Comedy` ],

  `Cost` : [ 10000 , 5000 , 15000 , 2000 ]})

 
# Print the data frame

print (df)

Output:

We will now use Dataframe.transpose () to find the transposition of this Dataframe.

# find transpose
df.transpose ()

Output:

As we can see from the output, Dataframe.transpose () successfully returned the transpose of the given Dataframe.

Get Solution for free from DataCamp guru