cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
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

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group