Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 39 additions & 10 deletions python/pyspark/sql/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,8 @@ def isEmpty(self) -> bool:
return self._jdf.isEmpty()

def show(self, n: int = 20, truncate: Union[bool, int] = True, vertical: bool = False) -> None:
"""Prints the first ``n`` rows to the console.
"""
Prints the first ``n`` rows of the DataFrame to the console.

.. versionadded:: 1.3.0

Expand All @@ -896,20 +897,32 @@ def show(self, n: int = 20, truncate: Union[bool, int] = True, vertical: bool =

Parameters
----------
n : int, optional
n : int, optional, default 20
Number of rows to show.
truncate : bool or int, optional
If set to ``True``, truncate strings longer than 20 chars by default.
truncate : bool or int, optional, default True
If set to ``True``, truncate strings longer than 20 chars.
If set to a number greater than one, truncates long strings to length ``truncate``
and align cells right.
vertical : bool, optional
If set to ``True``, print output rows vertically (one line
per column value).
If set to ``True``, print output rows vertically (one line per column value).

Examples
--------
>>> df = spark.createDataFrame([
... (14, "Tom"), (23, "Alice"), (16, "Bob")], ["age", "name"])
... (14, "Tom"), (23, "Alice"), (16, "Bob"), (19, "This is a super long name")],
... ["age", "name"])

Show :class:`DataFrame`

>>> df.show()
+---+--------------------+
|age| name|
+---+--------------------+
| 14| Tom|
| 23| Alice|
| 16| Bob|
| 19|This is a super l...|
+---+--------------------+

Show only top 2 rows.

Expand All @@ -922,6 +935,18 @@ def show(self, n: int = 20, truncate: Union[bool, int] = True, vertical: bool =
+---+-----+
only showing top 2 rows

Show full column content without truncation.

>>> df.show(truncate=False)
+---+-------------------------+
|age|name |
+---+-------------------------+
|14 |Tom |
|23 |Alice |
|16 |Bob |
|19 |This is a super long name|
+---+-------------------------+

Show :class:`DataFrame` where the maximum number of characters is 3.

>>> df.show(truncate=3)
Expand All @@ -931,20 +956,24 @@ def show(self, n: int = 20, truncate: Union[bool, int] = True, vertical: bool =
| 14| Tom|
| 23| Ali|
| 16| Bob|
| 19| Thi|
+---+----+

Show :class:`DataFrame` vertically.

>>> df.show(vertical=True)
-RECORD 0-----
-RECORD 0--------------------
age | 14
name | Tom
-RECORD 1-----
-RECORD 1--------------------
age | 23
name | Alice
-RECORD 2-----
-RECORD 2--------------------
age | 16
name | Bob
-RECORD 3--------------------
age | 19
name | This is a super l...
"""

if not isinstance(n, int) or isinstance(n, bool):
Expand Down