@@ -2289,7 +2289,100 @@ def memory_usage(self, index=True, deep=False):
2289
2289
return result
2290
2290
2291
2291
def transpose (self , * args , ** kwargs ):
2292
- """Transpose index and columns"""
2292
+ """
2293
+ Transpose index and columns.
2294
+
2295
+ Reflect the DataFrame over its main diagonal by writing rows as columns
2296
+ and vice-versa. The property :attr:`.T` is an accessor to the method
2297
+ :meth:`transpose`.
2298
+
2299
+ Parameters
2300
+ ----------
2301
+ copy : bool, default False
2302
+ If True, the underlying data is copied. Otherwise (default), no
2303
+ copy is made if possible.
2304
+ *args, **kwargs
2305
+ Additional keywords have no effect but might be accepted for
2306
+ compatibility with numpy.
2307
+
2308
+ Returns
2309
+ -------
2310
+ DataFrame
2311
+ The transposed DataFrame.
2312
+
2313
+ See Also
2314
+ --------
2315
+ numpy.transpose : Permute the dimensions of a given array.
2316
+
2317
+ Notes
2318
+ -----
2319
+ Transposing a DataFrame with mixed dtypes will result in a homogeneous
2320
+ DataFrame with the `object` dtype. In such a case, a copy of the data
2321
+ is always made.
2322
+
2323
+ Examples
2324
+ --------
2325
+ **Square DataFrame with homogeneous dtype**
2326
+
2327
+ >>> d1 = {'col1': [1, 2], 'col2': [3, 4]}
2328
+ >>> df1 = pd.DataFrame(data=d1)
2329
+ >>> df1
2330
+ col1 col2
2331
+ 0 1 3
2332
+ 1 2 4
2333
+
2334
+ >>> df1_transposed = df1.T # or df1.transpose()
2335
+ >>> df1_transposed
2336
+ 0 1
2337
+ col1 1 2
2338
+ col2 3 4
2339
+
2340
+ When the dtype is homogeneous in the original DataFrame, we get a
2341
+ transposed DataFrame with the same dtype:
2342
+
2343
+ >>> df1.dtypes
2344
+ col1 int64
2345
+ col2 int64
2346
+ dtype: object
2347
+ >>> df1_transposed.dtypes
2348
+ 0 int64
2349
+ 1 int64
2350
+ dtype: object
2351
+
2352
+ **Non-square DataFrame with mixed dtypes**
2353
+
2354
+ >>> d2 = {'name': ['Alice', 'Bob'],
2355
+ ... 'score': [9.5, 8],
2356
+ ... 'employed': [False, True],
2357
+ ... 'kids': [0, 0]}
2358
+ >>> df2 = pd.DataFrame(data=d2)
2359
+ >>> df2
2360
+ name score employed kids
2361
+ 0 Alice 9.5 False 0
2362
+ 1 Bob 8.0 True 0
2363
+
2364
+ >>> df2_transposed = df2.T # or df2.transpose()
2365
+ >>> df2_transposed
2366
+ 0 1
2367
+ name Alice Bob
2368
+ score 9.5 8
2369
+ employed False True
2370
+ kids 0 0
2371
+
2372
+ When the DataFrame has mixed dtypes, we get a transposed DataFrame with
2373
+ the `object` dtype:
2374
+
2375
+ >>> df2.dtypes
2376
+ name object
2377
+ score float64
2378
+ employed bool
2379
+ kids int64
2380
+ dtype: object
2381
+ >>> df2_transposed.dtypes
2382
+ 0 object
2383
+ 1 object
2384
+ dtype: object
2385
+ """
2293
2386
nv .validate_transpose (args , dict ())
2294
2387
return super (DataFrame , self ).transpose (1 , 0 , ** kwargs )
2295
2388
0 commit comments