Performing Basic Operations on DataFrames
Introduction to Basic DataFrame Operations
Welcome back! As you continue your journey in learning PySpark, understanding how to perform basic DataFrame operations is essential. In previous lessons, you learned about creating DataFrames and loading data into them. Today, we will take a step further by exploring some crucial operations: selecting columns, filtering rows, updating existing columns, and adding new columns. Mastering these operations will enable you to manipulate and analyze your data efficiently, making your datasets ready for more complex transformations and analyses.
Setting Up Environment and Dataset
To begin working with DataFrames, we must set up your PySpark environment by initializing a SparkSession and loading our dataset. In this lesson, we'll use a dataset named "employees.csv", which contains data on employee names, salaries, and departments.
Here's a quick look at the dataset:
With this data, we'll perform key DataFrame operations, including selecting, filtering, updating, and adding columns.
Selecting Specified Columns from DataFrames
Once your data is loaded into a DataFrame, you may not need every column for your analysis. You can select specific columns using the select method. For example, let's say you're interested in just the "Name" and "Salary" columns from your data.
You can achieve this with the following:
When executed, this code will show you the first few rows of the "Name" and "Salary" columns, helping you isolate the data relevant to your task.
