Violatic
Violatic

Reputation: 384

What function should I be using to round a column of a dataframe to a number?

I have a column in a pandas dataframe and I want to take all the numbers below 15,000 and either round them to 15,000 or 0.

I want to do the same with numbers above 200,000 and round them down to 200,000.

For rounding, .round() takes it to a number of significant figures and math.ceil()/.floor() take the numbers to the nearest integer but neither of these solve my problem on their own.

I assume there is some clever maths trick or a function I don't see.

Example:

Column_to_Round    Rounded_Column
11000              15000
9000               15000
3000               0
5000               0
16000              16000
220000             200000
199000             199000

Upvotes: 1

Views: 421

Answers (2)

Emil
Emil

Reputation: 1722

Write a function and use the apply function:

def rounding(number):
    if number < 15000:
        if number < 7500:
            return 0
        else return 15000
    elif number > 200000:
        return 200000
    else return number

dataset['Rounded_Column'] = dataset['Column_to_Round'].apply(rounding)

Upvotes: 3

jezrael
jezrael

Reputation: 863176

Use numpy.select for vectorized solution:

a = 15000
b = 200000
m1 = df['Column_to_Round'] < a / 2
m2 = df['Column_to_Round'] < a
m3 = df['Column_to_Round'] > b
df['new'] = np.select([m1, m2, m3],[0, 15000, 200000], default=df['Column_to_Round'])
print (df)
   Column_to_Round  Rounded_Column     new
0            11000           15000   15000
1             9000           15000   15000
2             3000               0       0
3             5000               0       0
4            16000           16000   16000
5           220000          200000  200000
6           199000          199000  199000

Upvotes: 2

Related Questions