Terence Zhong
Terence Zhong

Reputation: 301

use apply function to create a new column with round value in pandas

My dataframe like below:

name  |  salary 
Tom   |   10200 
Kate  |          
Mi    |   32311 

The value of kate is ' ' regarding to salary and round_salary, I replace its value with ' ', so it show nothing in cell.

Question:

I want to create a new salary column base on rounding the salary to the nearest 10,000.

The outcome would look like below

name  |  salary | round_salary
Tom   |   10200 |  10000
Kate  |         |  
Mi    |   32311 |  30000

my code shows below:

def round_income(salary):
    if '' in salary:
        return ''
    else: 
        return salary.round(decimals = -4)

income.apply(lambda x: round_salary(x['income']), axis=1)

the output error is :

KeyError: ('salary', 'occurred at index 0')

any one know how to fix it? I found map or apply function can solve it, thanks anyone's help in advance. ~

Upvotes: 1

Views: 739

Answers (1)

jezrael
jezrael

Reputation: 863186

Solution if no missing values but empty values for non numeric:

income['salary'] = (pd.to_numeric(income['salary'], errors='coerce')
                      .round(decimals = -4)
                      .fillna(''))
print (income)
   name salary
0   Tom  10000
1  Kate       
2    Mi  20000

Solution with missing values - all data in column salary are numeric:

income['salary'] = income['salary'].round(decimals = -4).astype('Int64')
print (income)
   name  salary
0   Tom   10000
1  Kate     NaN
2    Mi   20000

Upvotes: 1

Related Questions