pandas 1.4.2

NotesParametersReturns
transpose(self, *args, copy: 'bool' = False) -> 'DataFrame'

Reflect the DataFrame over its main diagonal by writing rows as columns and vice-versa. The property .T is an accessor to the method transpose .

Notes

Transposing a DataFrame with mixed dtypes will result in a homogeneous DataFrame with the :None:None:`object` dtype. In such a case, a copy of the data is always made.

Parameters

*args : tuple, optional

Accepted for compatibility with NumPy.

copy : bool, default False

Whether to copy the data after transposing, even for DataFrames with a single dtype.

Note that a copy is always required for mixed dtype DataFrames, or for DataFrames with any extension types.

Returns

DataFrame

The transposed DataFrame.

Transpose index and columns.

See Also

numpy.transpose

Permute the dimensions of a given array.

Examples

Square DataFrame with homogeneous dtype

This example is valid syntax, but we were not able to check execution
>>> d1 = {'col1': [1, 2], 'col2': [3, 4]}
... df1 = pd.DataFrame(data=d1)
... df1 col1 col2 0 1 3 1 2 4
This example is valid syntax, but we were not able to check execution
>>> df1_transposed = df1.T # or df1.transpose()
... 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:

This example is valid syntax, but we were not able to check execution
>>> df1.dtypes
col1    int64
col2    int64
dtype: object
This example is valid syntax, but we were not able to check execution
>>> df1_transposed.dtypes
0    int64
1    int64
dtype: object

Non-square DataFrame with mixed dtypes

This example is valid syntax, but we were not able to check execution
>>> d2 = {'name': ['Alice', 'Bob'],
...  'score': [9.5, 8],
...  'employed': [False, True],
...  'kids': [0, 0]}
... df2 = pd.DataFrame(data=d2)
... df2 name score employed kids 0 Alice 9.5 False 0 1 Bob 8.0 True 0
This example is valid syntax, but we were not able to check execution
>>> df2_transposed = df2.T # or df2.transpose()
... 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 :None:None:`object` dtype:

This example is valid syntax, but we were not able to check execution
>>> df2.dtypes
name         object
score       float64
employed       bool
kids          int64
dtype: object
This example is valid syntax, but we were not able to check execution
>>> df2_transposed.dtypes
0    object
1    object
dtype: object
See :

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /pandas/core/frame.py#3273
type: <class 'function'>
Commit: