How to slice a PySpark dataframe in two row-wise dataframe?
In this article, we are going to learn how to slice a PySpark DataFrame into two row-wise. Slicing a DataFrame is getting a subset containing all rows from one index to another.
Method 1: Using limit() and subtract() functions
In this method, we first make a PySpark DataFrame with precoded data using createDataFrame(). We then use limit() function to get a particular number of rows from the DataFrame and store it in a new variable. The syntax of limit function is :
Syntax : DataFrame.limit(num)
Returns : A DataFrame with num number of rows.
We will then use subtract() function to get the remaining rows from the initial DataFrame. The syntax of subtract function is :
Syntax : DataFrame1.subtract(DataFrame2)
Returns : A new DataFrame containing rows in DataFrame1 but not in DataFrame2.
Python
# Importing PySparkimport pysparkfrom pyspark.sql import SparkSession # Session CreationSpark_Session = SparkSession.builder.appName( 'Spark Session').getOrCreate() # Data filled in our DataFramerows = [['Lee Chong Wei', 69, 'Malaysia'], ['Lin Dan', 66, 'China'], ['Srikanth Kidambi', 9, 'India'], ['Kento Momota', 15, 'Japan']] # Columns of our DataFramecolumns = ['Player', 'Titles', 'Country'] # DataFrame is createddf = Spark_Session.createDataFrame(rows, columns) # Getting the slices# The first slice has 3 rowsdf1 = df.limit(3) # Getting the second slice by removing df1# from dfdf2 = df.subtract(df1) # Printing the first slicedf1.show() # Printing the second slice.df2.show() |
Output:

Method 2: Using randomSplit() function
In this method, we are first going to make a PySpark DataFrame using createDataFrame(). We will then use randomSplit() function to get two slices of the DataFrame while specifying the fractions of rows that will be present in both slices. The rows are split up RANDOMLY.
Syntax : DataFrame.randomSplit(weights,seed)
Parameters :
- weights : list of double values according to which the DataFrame is split.
- seed : the seed for sampling. This parameter is optional.
Returns : List of split DataFrames
Python
# Importing PySparkimport pysparkfrom pyspark.sql import SparkSession # Session CreationSpark_Session = SparkSession.builder.appName( 'Spark Session').getOrCreate() # Data filled in our DataFramerows = [['Lee Chong Wei', 69, 'Malaysia'], ['Lin Dan', 66, 'China'], ['Srikanth Kidambi', 9, 'India'], ['Kento Momota', 15, 'Japan']] # Columns of our DataFramecolumns = ['Player', 'Titles', 'Country'] #DataFrame is createddf = Spark_Session.createDataFrame(rows, columns) # the first slice has 20% of the rows# the second slice has 80% of the rows# the data in both slices is selected randomlydf1, df2 = df.randomSplit([0.20, 0.80]) # Showing the first slicedf1.show() # Showing the second slicedf2.show() |
Output:

Method 3: Using collect() function
In this method, we will first make a PySpark DataFrame using createDataFrame(). We will then get a list of Row objects of the DataFrame using :
DataFrame.collect()
We will then use Python List slicing to get two lists of Rows. Finally, we convert these two lists of rows to PySpark DataFrames using createDataFrame().
Python
# Importing PySpark and Pandasimport pysparkfrom pyspark.sql import SparkSessionimport pandas as pd # Session CreationSpark_Session = SparkSession.builder.appName( 'Spark Session').getOrCreate() # Data filled in our DataFramerows = [['Lee Chong Wei', 69, 'Malaysia'], ['Lin Dan', 66, 'China'], ['Srikanth Kidambi', 9, 'India'], ['Kento Momota', 15, 'Japan']] # Columns of our DataFramecolumns = ['Player', 'Titles', 'Country'] #DataFrame is createddf = Spark_Session.createDataFrame(rows, columns) # getting the list of Row objectsrow_list = df.collect() # Slicing the Python Listpart1 = row_list[:1]part2 = row_list[1:] # Converting the slices to PySpark DataFramesslice1 = Spark_Session.createDataFrame(part1)slice2 = Spark_Session.createDataFrame(part2) # Printing the first sliceprint('First DataFrame')slice1.show() # Printing the second sliceprint('Second DataFrame')slice2.show() |
Output:

Method 4: Converting PySpark DataFrame to a Pandas DataFrame and using iloc[] for slicing
In this method, we will first make a PySpark DataFrame using createDataFrame(). We will then convert it into a Pandas DataFrame using toPandas(). We then slice the DataFrame using iloc[] with the Syntax :
DataFrame.iloc[start_index:end_index]
The row at end_index is NOT included. Finally, we will convert our DataFrame slices to a PySpark DataFrame using createDataFrame()
Python
# Importing PySpark and Pandasimport pysparkfrom pyspark.sql import SparkSessionimport pandas as pd # Session CreationSpark_Session = SparkSession.builder.appName( 'Spark Session').getOrCreate() # Data filled in our DataFramerows = [['Lee Chong Wei', 69, 'Malaysia'], ['Lin Dan', 66, 'China'], ['Srikanth Kidambi', 9, 'India'], ['Kento Momota', 15, 'Japan']] # Columns of our DataFramecolumns = ['Player', 'Titles', 'Country'] # DataFrame is createddf = Spark_Session.createDataFrame(rows, columns) # Converting DataFrame to pandaspandas_df = df.toPandas() # First DataFrame formed by slicingdf1 = pandas_df.iloc[:2] # Second DataFrame formed by slicingdf2 = pandas_df.iloc[2:] # Converting the slices to PySpark DataFramesdf1 = Spark_Session.createDataFrame(df1)df2 = Spark_Session.createDataFrame(df2) # Printing the first sliceprint('First DataFrame')df1.show() # Printing the second sliceprint('Second DataFrame')df2.show() |
Output:



