Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
444 views
in Technique[技术] by (71.8m points)

apache spark - How to convert Pyspark Row datetime.datetime dataframe to column name DateType with timestamp in DD-MM-YYYY

I m working on NYK Green Taxi Data Jan 2017,I converted string column to Datetype now I can see only in row as alias , How to convert it into dataframe to Show() as table in Pickupdate column and DD-MM-YYYYFormat

-- lpep_pickup_datetime: string (nullable = true)

df2 = df.select(to_timestamp(df.lpep_pickup_datetime, 'yyyy-MM-dd HH:mm:ss').alias('pickup_datetime')).collect()

df2
[Row(pickup_datetime=datetime.datetime(2017, 1, 1, 0, 1, 15)), Row(pickup_datetime=datetime.datetime(2017, 1, 1, 0, 3, 34)), Row(pickup_datetime=datetime.datetime(2017, 1, 1, 0, 4, 2)), Row(pickup_datetime=datetime.datetime(2017, 1, 1, 0, 1, 40)), Row(pickup_datetime=datetime.datetime(2017, 1, 1, 0, 0, 51)), Row(pickup_datetime=datetime.datetime(2017, 1, 1, 0, 0, 28))

Output should be DD-MM-YYYY format Pickup_datetime Column like 1-1-2017 00:01:15

question from:https://stackoverflow.com/questions/65623153/how-to-convert-pyspark-row-datetime-datetime-dataframe-to-column-name-datetype-w

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Don't do collect which will convert your dataframe into a list of Rows. Just do the select, which will return a new dataframe df2, and call show() on that new dataframe.

df2 = df.select(to_timestamp(df.lpep_pickup_datetime, 'yyyy-MM-dd HH:mm:ss').alias('pickup_datetime'))

df2.show()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...