Dataframes is a buzzword in the Industry nowadays. People tend to use it with popular languages used for Data Analysis like Python, Scala and R. Plus, with the evident need for handling complex analysis and munging tasks for Big Data, Python for Spark or PySpark Certification has become one of the most sought-after skills in the industry today. So, why is it that everyone is using it so much? Let’s understand this with our PySpark Dataframe Tutorial blog. In this blog I’ll be covering the following topics:

PySpark Dataframe Tutorial: What are Dataframes?

Dataframes generally refers to a data structure, which is tabular in nature. It represents Rows, each of which consists of a number of observations. Rows can have a variety of data formats (Heterogeneous), whereas a column can have data of the same data type (Homogeneous). Data frames usually contain some metadata in addition to data; for example, column and row names.

We can say that Dataframes are nothing, but 2-Dimensional Data Structure, similar to an SQL table or a spreadsheet. Now let’s move ahead with this PySpark Dataframe Tutorial and understand why exactly we need Pyspark Dataframe?

Why do we need Dataframes?

1.Processing Structured and Semi-Structured Data

Dataframes are designed to process a large collection of structured as well as Semi-Structured data. Observations in Spark DataFrame are organized under named columns, which helps Apache Spark to understand the schema of a DataFrame. This helps Spark optimize execution plan on these queries. It can also handle Petabytes of data.

2.Slicing and Dicing

Data frame APIs usually supports elaborate methods for slicing-and-dicing the data. It includes operations such as “selecting” rows, columns, and cells by name or by number, filtering out rows, etc. Statistical data is usually very messy and contain lots of missing and wrong values and range violations. So a critically important feature of data frames is the explicit management of missing data.

3.Data Sources

DataFrame has a support for a wide range of data format and sources, we’ll look into this later on in this Pyspark Dataframe Tutorial blog. They can take in data from various sources.

4.Support for Multiple Languages

It has API support for different languages like Python, R, Scala, Java, which makes it easier to be used by people having different programming backgrounds.

Subscribe to our youtube channel to get new updates..!

Features of Dataframes

Dataframes are Distributed in Nature, which makes it Fault Tolerant and Highly Available Data Structure.

Lazy evaluation is an evaluation strategy which holds the evaluation of an expression until its value is needed. It avoids repeated evaluation. Lazy evaluation in Spark means that the execution will not start until an action is triggered. In Spark, the picture of lazy evaluation comes when Spark transformations occur.

Dataframes are Immutable in nature. By immutable I mean that it is an object whose state cannot be modified after it is created. But we can transform its values by applying a certain transformation, like in RDDs.

PySpark Dataframe Sources

Dataframes in Pyspark can be created in multiple ways:

Data can be loaded in through a CSV, JSON, XML or a Parquet file. It can also be created using an existing RDD and through any other database, like Hive or Cassandra as well. It can also take in data from HDFS or the local file system.

Dataframe Creation

Let’s move forward with this PySpark Dataframe Tutorial blog and understand how to create Dataframes.

Pyspark Dataframes Example 1: FIFA World Cup Dataset

Here we have taken FIFA World Cup Players Dataset. We are going to load this data which is in CSV format into a dataframe and then we’ll learn about the different transformations and actions that can be performed on this dataframe.

Reading Data from CSV file

Let’s load the data from a CSV file. Here we are going to use the spark.read.csv method to load the data into a dataframe fifa_df. The actual method is spark.read.format[csv/json].

Schema of Dataframe

To have a look at the schema ie. the structure of the dataframe, we’ll use the printSchemamethod. This will give us the different columns in our dataframe along with the data type and the nullable conditions for that particular column.

fifa_df.printSchema()

Column Names and Count (Rows and Column)

When we want to have a look at the names and a count of the number of Rows and Columns of a particular Dataframe, we use the following methods.

Describing a Particular Column

If we want to have a look at the summary of any particular column of a Dataframe, we use the describe method. This method gives us the statistical summary of the given column, if not specified, it provides the statistical summary of the dataframe.

Selecting Multiple Columns

If we want to select particular columns from the dataframe, we use the selectmethod.

fifa_df.select('Player Name','Coach Name').show()

Selecting Distinct Multiple Columns

fifa_df.select('Player Name','Coach Name').distinct().show()

Filtering Data

In order to filter the data, according to the condition specified, we use the filter command. Here we are filtering our dataframe based on the condition that Match ID must be equal to 1096 and then we are calculating how many records/rows are there in the filtered output.

fifa_df.filter(fifa_df.MatchID=='1096').show()
fifa_df.filter(fifa_df.MatchID=='1096').count() //to get the count

Filtering the Data

Grouping the Data

GroupByis used to group the dataframe based on the column specified. Here, we are grouping the dataframe based on the column Race and then with the countfunction, we can find the count of the particular race.

Race_df = Superhero_df.groupby("Race")
.count()
.show()

Performing SQL Queries

We can also pass SQL queries directly to any dataframe, for that we need to create a table from the dataframe using the registerTempTablemethod and then use the sqlContext.sql() to pass the SQL queries.

I hope you guys got an idea of what PySpark Dataframe is, why is it used in the industry and its features in this PySpark Dataframe Tutorial Blog. Congratulations, you are no longer a Newbie to Dataframes. If you want to learn more about PySpark and Understand the different Industry Use Cases, have a look at our Spark with Python andPySpark Tutorial Blog.