Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python - convert loc expression from pandas to Pyspark?

How can I convert this expression from pandas to Pyspark Dataframe?

The target is to assign the column date_stamp the value cur

#the data frame is:
tag,     2020-06-25
-------------------
3FMTK1RM    0
678jhgt     18

#######################
vin='3FMTK1RM'# is the first element of tag
cur= 5
date_stamp='2020-06-25'
df.loc[str(date_stamp),vin] = cur
question from:https://stackoverflow.com/questions/66061625/convert-loc-expression-from-pandas-to-pyspark

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

1 Reply

0 votes
by (71.8m points)

You can use when:

import pyspark.sql.functions as F

df2 = df.withColumn(
    '2020-06-25', 
    F.when(F.col('tag') == 'vin', cur).otherwise(F.col('2020-06-25'))
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...