The .toPandas() action

The .toPandas() action, as the name suggests, converts the Spark DataFrame into a pandas DataFrame. The same warning needs to be issued here as with the .collect() action – the .toPandas() action collects all the records from all the workers, returns them to the driver, and then converts the results into a pandas DataFrame. 

Since our sample data is tiny, we can do this without any problems:

sample_data_schema.toPandas()

This is what the results look like:

Get PySpark Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.