Quickstart: Pandas API on Spark

This is a short introduction to pandas API on Spark, geared mainly for new users. This notebook shows you some key differences between pandas and pandas API on Spark. You can run this examples by yourself in 'Live Notebook: pandas API on Spark' at the quickstart page.

Customarily, we import pandas API on Spark as follows:

Object Creation

Creating a pandas-on-Spark Series by passing a list of values, letting pandas API on Spark create a default integer index:

Creating a pandas-on-Spark DataFrame by passing a dict of objects that can be converted to series-like.

Creating a pandas DataFrame by passing a numpy array, with a datetime index and labeled columns:

Now, this pandas DataFrame can be converted to a pandas-on-Spark DataFrame

It looks and behaves the same as a pandas DataFrame.

Also, it is possible to create a pandas-on-Spark DataFrame from Spark DataFrame easily.

Creating a Spark DataFrame from pandas DataFrame

Creating pandas-on-Spark DataFrame from Spark DataFrame.

Having specific dtypes . Types that are common to both Spark and pandas are currently supported.

Here is how to show top rows from the frame below.

Note that the data in a Spark dataframe does not preserve the natural order by default. The natural order can be preserved by setting compute.ordered_head option but it causes a performance overhead with sorting internally.

Displaying the index, columns, and the underlying numpy data.

Showing a quick statistic summary of your data

Transposing your data

Sorting by its index

Sorting by value

Missing Data

Pandas API on Spark primarily uses the value np.nan to represent missing data. It is by default not included in computations.

To drop any rows that have missing data.

Filling missing data.

Operations

Stats

Performing a descriptive statistic:

Spark Configurations

Various configurations in PySpark could be applied internally in pandas API on Spark. For example, you can enable Arrow optimization to hugely speed up internal pandas conversion. See also PySpark Usage Guide for Pandas with Apache Arrow in PySpark documentation.

Grouping

By “group by” we are referring to a process involving one or more of the following steps:

Grouping and then applying the sum() function to the resulting groups.

Grouping by multiple columns forms a hierarchical index, and again we can apply the sum function.

Plotting

On a DataFrame, the plot() method is a convenience to plot all of the columns with labels:

For more details, Plotting documentation.

Getting data in/out

CSV

CSV is straightforward and easy to use. See here to write a CSV file and here to read a CSV file.

Parquet

Parquet is an efficient and compact file format to read and write faster. See here to write a Parquet file and here to read a Parquet file.

Spark IO

In addition, pandas API on Spark fully supports Spark's various datasources such as ORC and an external datasource. See here to write it to the specified datasource and here to read it from the datasource.

See the Input/Output documentation for more details.