In order to convert a column to Upper case in pyspark we will be using upper() function, to convert a column to Lower case in pyspark is done using lower() function, and in order to convert to title case or proper case in pyspark uses initcap() function. Let’s see an example of each.
- Convert column to upper case in pyspark – upper() function
- Convert column to lower case in pyspark – lower() function
- Convert column to title case or proper case in pyspark – initcap() function
We will be using dataframe df_states
Convert column to upper case in pyspark – upper() Function :
Syntax:
colname1 – Column name
upper() Function takes up the column name as argument and converts the column to upper case.
########## convert column to upper case in pyspark from pyspark.sql.functions import upper, col df_states.select("*", upper(col('state_name'))).show()
column “state_name” is converted to upper case as shown below
Convert column to Lower case in pyspark – lower() function :
Syntax:
colname1 – Column name
lower() Function takes up the column name as argument and converts the column to lower case
########## convert column to lower case in pyspark from pyspark.sql.functions import lower, col df_states.select("*", lower(col('state_name'))).show()
column “state_name” is converted to lower case as shown below
Convert column to Title or proper case in pyspark – initcap() function:
Syntax:
colname1 – Column name
initcap() Function takes up the column name as argument and converts the column to title case or proper case
########## convert column to title case from pyspark.sql.functions import initcap, col df_states.select("*", initcap(col('state_name'))).show()
column “state_name” is converted to title case or proper case as shown below,
Other Related Topics:
- Remove leading zero of column in pyspark
- Left and Right pad of column in pyspark –lpad() & rpad()
- Add Leading and Trailing space of column in pyspark – add space
- Remove Leading, Trailing and all space of column in pyspark – strip & trim space
- String split of the columns in pyspark
- Repeat the column in Pyspark
- Get Substring of the column in Pyspark
- Get String length of column in Pyspark
- Typecast string to date and date to string in Pyspark
- Typecast Integer to string and String to integer in Pyspark
- Convert to upper case, lower case and title case in pyspark
- Extract First N and Last N character in pyspark
- Add leading zeros to the column in pyspark
- Concatenate two columns in pyspark