Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I have a date column in a pyspark dataframe that I want to change the title of and extract only the last 8 characters from while preserving its order

my dataframe looks like this:

| accountId | income | dateOfOrder 
| 123       | 60000  | 56347264327_01_20200110
| 321       | 52000  | 54346262452_01_20200218

I want to take the header dateOfOrder and change it to acct_order_dt and only use the last 8 characters which are dates in yyyymmdd. I want to preserve the order of this pyspark dataframe.

I am currently using this method but I dont think it’s preserving the order:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

sample_data = sample_data.withColumn("acct_order_dt", to_date(substring(col("dateOfOrder"),-8,8), "yyyyMMdd")).drop("dateOfOrder")

>Solution :

To achieve this, you can use the withColumn method and the substring function. Here’s an example code in PySpark:

from pyspark.sql.functions import substring

df = df.withColumn("acct_order_dt", substring(df["dateOfOrder"], -8, 8))
df = df.drop("dateOfOrder")
df = df.selectExpr("accountId", "income", "acct_order_dt")

The first line uses withColumn to add a new column acct_order_dt to the DataFrame. The substring function is used to extract the last 8 characters of the dateOfOrder column and store them in acct_order_dt.

scala apache Spark snippet :
import org.apache.spark.sql.functions._

val df2 = df.withColumn("acct_order_dt", substring(col("dateOfOrder"), -8, 8))
  .drop("dateOfOrder")
  .select("accountId", "income", "acct_order_dt")

since you got the 8 characters you can apply any sql function of your choice to convert your required date format

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading