Skip to content

Commit c4a06fe

Browse files
committed
DOC: Add more complete example to DataFrame.transpose()
1 parent 73c5c6b commit c4a06fe

File tree

1 file changed

+32
-5
lines changed

1 file changed

+32
-5
lines changed

pandas/core/frame.py

+32-5
Original file line numberDiff line numberDiff line change
@@ -2315,22 +2315,49 @@ def transpose(self, *args, **kwargs):
23152315
23162316
Examples
23172317
--------
2318-
>>> d = {'col1': [1, 2], 'col2': [3, 4]}
2319-
>>> df = pd.DataFrame(data=d)
2320-
>>> df
2318+
>>> d1 = {'col1': [1, 2], 'col2': [3, 4]}
2319+
>>> df1 = pd.DataFrame(data=d1)
2320+
>>> df1
23212321
col1 col2
23222322
0 1 3
23232323
1 2 4
23242324
2325-
>>> df.T
2325+
>>> df1.T
23262326
0 1
23272327
col1 1 2
23282328
col2 3 4
23292329
2330-
>>> df.transpose()
2330+
>>> df1.transpose()
23312331
0 1
23322332
col1 1 2
23332333
col2 3 4
2334+
2335+
**Non-square DataFrame and mixed data types**
2336+
2337+
>>> d2 = {'name': ['Alice', 'Bob'],
2338+
... 'score': [9.5, 8],
2339+
... 'employed': [False, True],
2340+
... 'kids': [0, 0]}
2341+
2342+
>>> df2 = pd.DataFrame(data=d2)
2343+
>>> df2
2344+
name score employed kids
2345+
0 Alice 9.5 False 0
2346+
1 Bob 8.0 True 0
2347+
2348+
>>> df2.T
2349+
0 1
2350+
name Alice Bob
2351+
score 9.5 8
2352+
employed False True
2353+
kids 0 0
2354+
2355+
>>> df2.transpose()
2356+
0 1
2357+
name Alice Bob
2358+
score 9.5 8
2359+
employed False True
2360+
kids 0 0
23342361
"""
23352362
nv.validate_transpose(args, dict())
23362363
return super(DataFrame, self).transpose(1, 0, **kwargs)

0 commit comments

Comments
 (0)