access_time 2 months ago languageEnglish
more_vert

Spark DataFrame: Show Full Column Content

visibility 17 comment 0

When calling Spark show function to display the content of a DataFrame, it will not print out the full content of a column by default.  For example, the following output prints out truncated column content:

+---+--------------------+
| ID|               Value|
+---+--------------------+
|  1|Adddddddddddddddd...|
|  1|                   A|
|  3|                   C|
+---+--------------------+

To show the full content of the column, we just need to specify the truncate parameter to False:

:param truncate: If set to ``True``, truncate strings longer than 20 chars by default.
            If set to a number greater than one, truncates long strings to length ``truncate``
            and align cells right.

Code snippet

from pyspark.sql import SparkSession
from pyspark.sql.types import IntegerType, StringType, StructField, StructType

appName = "PySpark Example - Show Column Full Content"
master = "local"

# Create Spark session
spark = SparkSession.builder \
    .appName(appName) \
    .master(master) \
    .getOrCreate()

# Sample data
data = [(1, 'Addddddddddddddddddddddddddddddddddddddddddd'),
        (1, 'A'), (3, 'C')]

# schema
schema = StructType([StructField("ID", IntegerType(), True),
                     StructField("Value", StringType(), True)])

# Create Spark DaraFrame from pandas DataFrame
df = spark.createDataFrame(data, schema)
df.show(truncate=False)

spark.stop()

Output:

+---+--------------------------------------------+
|ID |Value                                       |
+---+--------------------------------------------+
|1  |Addddddddddddddddddddddddddddddddddddddddddd|
|1  |A                                           |
|3  |C                                           |
+---+--------------------------------------------+
copyright This page is subject to Site terms.
Like this article?
Share on

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

Follow Kontext

Get our latest updates on LinkedIn or Twitter.

Want to contribute on Kontext to help others?

Learn more