I have a string column in df which contains date in dd/MM/yyyy
format and I want to convert that format to yyyy-MM-dd
using with column
Asked
Active
Viewed 853 times
0
2 Answers
1
If you know you will have a consistent format in your column, you can pass this to 'to_datetime'.
You can try like below-
df['column_name'] = pd.to_datetime(df['column_name'], format='%d/%m/%y').dt.strftime('%Y-%m-%d')

Manish Kumar
- 162
- 3
-
it is pyspark dataframe not pandas – sriram9ma Jun 23 '21 at 16:59
-
PySpark SQL function provides to_date() function to convert String to Date fromat of a DataFrame column. to_date() – function is used to format string (StringType) to date (DateType) column. Refer to [Link](https://sparkbyexamples.com/pyspark/pyspark-to_date-convert-string-to-date-format/) – Manish Kumar Jun 23 '21 at 17:10
0
Using python's datetime lib it can be done accordingly:
from datetime import datetime
date_string = '10/10/2000'
datetime_object = datetime.strptime(date_string, '%d/%m/%Y')
converted_date_string = datetime_object.strftime('%Y-%m-%d')

Anders Elmgren
- 637
- 5
- 12
-
-
Could the answer to this question help? https://stackoverflow.com/questions/50607059/pyspark-date-yyyy-mmm-dd-conversion Something along this line- `df = df.withColumn('date_col', f.udf(lambda d: strptime(d, '%d/%m/%Y').strftime('%Y-%m-%d'), t.StringType())(f.col('date_col')))` – Anders Elmgren Jun 23 '21 at 19:53