python中transpose函数_Python Pandas Series.transpose()用法及代码示例

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

Pandas Series.transpose()函数返回转置,按定义是self。

用法: Series.transpose(*args, **kwargs)

参数:没有

返回:自

范例1:采用Series.transpose()函数查找给定Series对象的转置。

# 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.transpose()函数查找给定系列对象的转置。

# find the transpose

sr.transpose()

输出:

正如我们在输出中看到的,Series.transpose()函数已返回与给定系列对象的转置相同的对象,按定义为self。

范例2:采用Dataframe.transpose()函数查找给定数据帧的转置。

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

输出:

现在我们将使用Dataframe.transpose()函数查找给定数据帧的转置。

# find the transpose

df.transpose()

输出:

正如我们在输出中看到的,Dataframe.transpose()函数已成功返回给定Dataframe对象的转置。

你可能感兴趣的:(python中transpose函数_Python Pandas Series.transpose()用法及代码示例)