pandas.DataFrame.T屬性用於轉置 DataFrame 的索引和列。屬性T以某種方式與方法transpose()有關。此屬性的主要函數是通過將行設為列,反之亦然,在主對角線上創建數據幀的反射。
用法:DataFrame.T
參數:
copy:如果為True,則複製基礎數據,否則(默認)。
* args,** kwargs:其他關鍵字
Returns:轉置數據幀
範例1:
有時我們需要轉置 DataFrame 架以便更準確地研究它。在這種情況下,pandas.DataFrame.T屬性起著重要作用。
Python3
# Importing pandas module
import pandas as pd
# Creating a dictionary
dit = {'August':[10, 25, 34, 4.85, 71.2, 1.1],
'September':[4.8, 54, 68, 9.25, 58, 0.9],
'October':[78, 5.8, 8.52, 12, 1.6, 11],
'November':[100, 5.8, 50, 8.9, 77, 10]}
# Converting it to data frame
df = pd.DataFrame(data=dit)
# Original DataFrame
df
輸出:
轉置數據幀。
Python3
# Transposing the data frame
# using dataframe.T property
df_trans = df.T
print("Transposed Data frame:")
df_trans
輸出:
在上麵的示例中,我們轉置了具有數值/內容的數據幀‘df’。
範例2:
Python3
# Import pandas library
import pandas as pd
# initialize list of lists
data = [['Harvey.', 10.5, 45.25, 95.2],
['Carson', 15.2, 54.85, 50.8],
['juli', 14.9, 87.21, 60.4],
['Ricky', 20.3, 45.23, 99.5],
['Gregory', 21.1, 77.25, 90.9],
['Jessie', 16.4, 95.21, 10.85]]
# Create the pandas DataFrame
df = pd.DataFrame(data, columns=['Name', 'Age', 'Percentage', 'Accuracy'],
index=['a', 'b', 'c', 'd', 'e', 'f'])
# print dataframe.
df
輸出:
轉置 DataFrame 。
Python3
# Transposing the data frame
# using dataframe.T property
df_trans = df.T
print("Transposed Data frame:")
df_trans
輸出:
在上麵的示例中,我們轉置了具有混合數據類型的數據幀‘df’。
相關用法
- Python Wand function()用法及代碼示例
- Python Sorted()用法及代碼示例
- Python Numbers choice()用法及代碼示例
- Python Tkinter askopenfile()用法及代碼示例
- Python ord()用法及代碼示例
- Python sum()用法及代碼示例
- Python round()用法及代碼示例
- Python id()用法及代碼示例
- Python vars()用法及代碼示例
注:本文由純淨天空篩選整理自vanshgaur14866大神的英文原創作品 pandas.DataFrame.T() function in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。