首页 > 解决方案 > 如何检查数据框列是否包含数值?蟒蛇 3.6

问题描述

我有两列的数据框 - REGIONID & REGIONNAME

如果 REGIONID 包含数字值,我想用 REGIONNAME 更新 REGIONID。

Data_All.loc[Data_All['REGIONID'].str.isnumeric() is True , 'REGIONID'] = Data_All['REGIONNAME']

我收到类似的错误

"KeyError: 'cannot use a single bool to index into setitem'"

标签: pythonpython-3.xpandasdataframe

解决方案


删除is True因为isnumeric返回布尔掩码:

Data_All.loc[Data_All['REGIONID'].str.isnumeric(), 'REGIONID'] = Data_All['REGIONNAME']

如有必要,请检查Trues(例如NaN,列中的 s REGIONID):

Data_All.loc[Data_All['REGIONID'].str.isnumeric() == True, 'REGIONID'] = Data_All['REGIONNAME']

推荐阅读