I've got a list of column names i want to sum
columns = ['col1','col2','col3']
How can i add the three and put it in a new column ? (in an automatic way, so that i can change the column list and have new results)
Dataframe with result i want:
col1 col2 col3 result
1 2 3 6
Thanks !
Try this:
df = df.withColumn('result', sum(df[col] for col in df.columns))
df.columns
will be list of columns from df.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments