mars.dataframe.DataFrame.transpose#
- DataFrame.transpose()#
Transpose index and columns.
Reflect the DataFrame over its main diagonal by writing rows as columns and vice-versa. The property
T
is an accessor to the methodtranspose()
.- 参数
*args (tuple, optional) – Accepted for compatibility with NumPy.
- 返回
The transposed DataFrame.
- 返回类型
参见
numpy.transpose
Permute the dimensions of a given array.
提示
Transposing a DataFrame with mixed dtypes will result in a homogeneous DataFrame with the object dtype.
实际案例
Square DataFrame with homogeneous dtype
>>> import mars.dataframe as md >>> d1 = {'col1': [1, 2], 'col2': [3, 4]} >>> df1 = md.DataFrame(data=d1).execute() >>> df1 col1 col2 0 1 3 1 2 4
>>> df1_transposed = df1.T.execute() # or df1.transpose().execute() >>> df1_transposed 0 1 col1 1 2 col2 3 4
When the dtype is homogeneous in the original DataFrame, we get a transposed DataFrame with the same dtype:
>>> df1.dtypes col1 int64 col2 int64 dtype: object
>>> df1_transposed.dtypes 0 int64 1 int64 dtype: object
Non-square DataFrame with mixed dtypes
>>> d2 = {'name': ['Alice', 'Bob'], ... 'score': [9.5, 8], ... 'employed': [False, True], ... 'kids': [0, 0]} >>> df2 = md.DataFrame(data=d2).execute() >>> df2 name score employed kids 0 Alice 9.5 False 0 1 Bob 8.0 True 0
>>> df2_transposed = df2.T.execute() # or df2.transpose().execute() >>> df2_transposed 0 1 name Alice Bob score 9.5 8.0 employed False True kids 0 0
When the DataFrame has mixed dtypes, we get a transposed DataFrame with the object dtype:
>>> df2.dtypes name object score float64 employed bool kids int64 dtype: object
>>> df2_transposed.dtypes 0 object 1 object dtype: object