PySpark – Extracting single value from DataFrame
Last Updated :
17 Jun, 2021
In this article, we are going to extract a single value from the pyspark dataframe columns. To do this we will use the first() and head() functions.
Single value means only one value, we can extract this value based on the column name
Syntax:
- dataframe.first()[‘column name’]
- Dataframe.head()[‘Index’]
Where,
- dataframe is the input dataframe and column name is the specific column
- Index is the row and columns.
So we are going to create the dataframe using the nested list.
Python3
import pyspark
from pyspark.sql import SparkSession
spark = SparkSession.builder.appName( 'sparkdf' ).getOrCreate()
data = [[ "1" , "sravan" , "vignan" ],
[ "2" , "ojaswi" , "vvit" ],
[ "3" , "rohith" , "vvit" ],
[ "4" , "sridevi" , "vignan" ],
[ "1" , "sravan" , "vignan" ],
[ "5" , "gnanesh" , "iit" ]]
columns = [ 'student ID' , 'student NAME' , 'college' ]
dataframe = spark.createDataFrame(data,columns)
print ( "Actual data in dataframe" )
dataframe.show()
|
Output:
Actual data in dataframe
+----------+------------+-------+
|student ID|student NAME|college|
+----------+------------+-------+
| 1| sravan| vignan|
| 2| ojaswi| vvit|
| 3| rohith| vvit|
| 4| sridevi| vignan|
| 1| sravan| vignan|
| 5| gnanesh| iit|
+----------+------------+-------+
Example 1: Python program to extract a single value from a particular column using first().
Python3
dataframe.first()[ 'student ID' ]
|
Output:
'1'
Example 2: Extract a single value using head().
Output:
'1'
Example 3: Extract a single value using head().
Output:
'vignan'
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...