cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
cancel
Showing results for 
Search instead for 
Did you mean: 

issue with rounding selected column in "for in" loop

Braxx
Contributor II

This must be trivial, but I must have missed something.

I have a dataframe (test1) and want to round all the columns listed in list of columns (col_list)

imagehere is the code I am running:

col_list = ['measure1', 'measure2', 'measure3']
 
for i in col_list:
  rounding = test1\
    .withColumn(i, round(col(i),0))
 
display(rounding)

and as a result only the last column has its values rounded.

imageWhat am I missing to have all the measures rounded?

data for testing:

car model measure1 measure2 measure3

Nissan aa 1.11 0.3 34

Toyota bb 1.12 0.4 111

BMW cc 1.13 0.5 1.9

1 ACCEPTED SOLUTION

Accepted Solutions

AmanSehgal
Honored Contributor III

Because it's a for loop, and in the last loop column measure3 is selected. The variable rounding is assigned a new dataframe with changes that occur on column measure3 only.

Try the following code:

rounding = test1
for i in col_list:
  rounding = rounding\
    .withColumn(i, round(col(i),0))

View solution in original post

2 REPLIES 2

AmanSehgal
Honored Contributor III

Because it's a for loop, and in the last loop column measure3 is selected. The variable rounding is assigned a new dataframe with changes that occur on column measure3 only.

Try the following code:

rounding = test1
for i in col_list:
  rounding = rounding\
    .withColumn(i, round(col(i),0))

Braxx
Contributor II

You're absolutely right. thanks

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.