Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用“应用”函数创建一个新列,该列的值为整数_Python_Pandas_Pandas Apply - Fatal编程技术网

Python 使用“应用”函数创建一个新列,该列的值为整数

Python 使用“应用”函数创建一个新列,该列的值为整数,python,pandas,pandas-apply,Python,Pandas,Pandas Apply,我的数据帧如下所示: name | salary Tom | 10200 Kate | Mi | 32311 kate的值是“”,关于薪水和舍入工资,我将其值替换为“”,因此它在单元格中不显示任何内容 问题: 我想创建一个新的薪资列,将薪资四舍五入到最接近的10000 结果如下所示 name | salary | round_salary Tom | 10200 | 10000 Kate | | Mi

我的数据帧如下所示:

name  |  salary 
Tom   |   10200 
Kate  |          
Mi    |   32311 
kate的值是“”,关于薪水和舍入工资,我将其值替换为“”,因此它在单元格中不显示任何内容

问题:

我想创建一个新的薪资列,将薪资四舍五入到最接近的10000

结果如下所示

name  |  salary | round_salary
Tom   |   10200 |  10000
Kate  |         |  
Mi    |   32311 |  30000
我的代码如下所示:

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

income.apply(lambda x: round_salary(x['income']), axis=1)
输出错误为:

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

有人知道怎么修吗?我发现map或apply函数可以解决这个问题,提前谢谢大家的帮助~

如果没有缺失值,但非数值的值为空,则解决方案:

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
缺少值的解决方案-列
薪资
中的所有数据都是数字:

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