ColumnRound
Rounds each value in one or more columns up or down to the nearest multiple of a number. The number being rounded to can be an integer or decimal number. The default multiple is 1, rounding each number to the nearest integer. If a different number is specified, say 2.5, then each number would be rounded to the nearest multiple of 2.5 -> 0, 2.5, 5, 7.5, 10, etc.
Options
columns: Specifies columns to round
where: Specifies a condition for rounding columns
to: Specifies the number of decimal places to round to
Examples
Example 1 - Round Single Column
When you only want to round one column instead of the entire dataset, specify the column name. In this case, we round the AccountBalance column to the nearest whole number to simplify the balance values.
#> ColumnRound AccountBalance
AFLEFT
bankTransactionsDf['AccountBalance'] = round(bankTransactionsDf['AccountBalance']) AFRIGHT
Example 2 - Round Multiple Columns
To round multiple specific columns without affecting the rest of the dataframe, list each column you want to round. In this example, we round both AccountBalance and TransactionAmount to the nearest whole number.
#> ColumnRound AccountBalance TransactionAmount
AFLEFT
bankTransactionsDf['AccountBalance'] = round(bankTransactionsDf['AccountBalance'])
bankTransactionsDf['TransactionAmount'] = round(bankTransactionsDf['TransactionAmount']) AFRIGHT
Example 3 - Round to Nearest Multiple
Instead of rounding to whole numbers, you can round to the nearest multiple of a specific value. In this case, we round both TransactionAmount and AccountBalance to the nearest multiple of 5.5.
#> ColumnRound --columns TransactionAmount AccountBalance --to 5.5
AFLEFT
bankTransactionsDf['TransactionAmount'] = round(bankTransactionsDf['TransactionAmount'] / 5.5) * 5.5
bankTransactionsDf['AccountBalance'] = round(bankTransactionsDf['AccountBalance'] / 5.5) * 5.5 AFRIGHT
Example 4 - Conditional Rounding to Nearest Multiple
In some cases, we want to round only certain rows based on a condition. This example rounds TransactionAmount and TransactionDuration to the nearest 10, but only for rows where the TransactionAmount is greater than 100.
#> ColumnRound --columns TransactionAmount TransactionDuration --to 10 --where TransactionAmount > 100
AFLEFT
bankTransactionsDf['TransactionAmount'][bankTransactionsDf['TransactionAmount'] > 100] = round(bankTransactionsDf['TransactionAmount'] / 10) * 10
bankTransactionsDf['TransactionDuration'][bankTransactionsDf['TransactionAmount'] > 100] = round(bankTransactionsDf['TransactionDuration'] / 10) * 10 AFRIGHT