python - 如何在 python pandas 的整数列上使用 .map
问题描述
我正在尝试获取一个整数列并将离散值映射到另一列。基本上,如果标记了信用等级,则 1、2、3、antoher 列将它们映射到no credit state
或。然后用 填充空值。我试过但是,我不断收到此错误:no hit
thin files
vaild
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-129-926e6625f2b6> in <module>
1 #train.dtypes
----> 2 df['discrete_52278'] = df.apply(lambda row: discrete_credit(row, 'credit_52278'), axis = 1)
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in apply(self, func, axis, broadcast, raw, reduce, result_type, args, **kwds)
6012 args=args,
6013 kwds=kwds)
-> 6014 return op.get_result()
6015
6016 def applymap(self, func):
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\apply.py in get_result(self)
140 return self.apply_raw()
141
--> 142 return self.apply_standard()
143
144 def apply_empty_result(self):
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\apply.py in apply_standard(self)
246
247 # compute the result using the series generator
--> 248 self.apply_series_generator()
249
250 # wrap results
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\apply.py in apply_series_generator(self)
275 try:
276 for i, v in enumerate(series_gen):
--> 277 results[i] = self.f(v)
278 keys.append(v.name)
279 except Exception as e:
<ipython-input-129-926e6625f2b6> in <lambda>(row)
1 #train.dtypes
----> 2 df['discrete_52278'] = df.apply(lambda row: discrete_credit(row, 'credit_52278'), axis = 1)
<ipython-input-126-462888d46184> in discrete_credit(row, variable)
6
7 """
----> 8 score = row[variable].map({1:'no_credit_state', 2:'thin_file', 3:"no_hit"})
9 score = row[score].fillna('valid')
10 score = pd.Categorical(row[score], ['valid', 'no_credit_state','thin_file', 'no_hit'])
AttributeError: ("'numpy.int64' object has no attribute 'map'", 'occurred at index 0')
这是一个引发相同错误的代码示例:
import pandas as pd
credit = {'credit_52278':[1,2,3,500,550,600,650,700,750,800,900]
}
df = pd.DataFrame(credit)
def discrete_credit(row, variable):
"""
allows thin files, no hits and no credit scores to float which will then allow the rest of the credit score to be fit \
with a spline
"""
score = row[variable].map({1:'no_credit_state', 2:'thin_file', 3:"no_hit"})
score = row[score].fillna('valid')
score = pd.Categorical(row[score], ['valid', 'no_credit_state','thin_file', 'no_hit'])
return score
df['discrete_52278'] = df.apply(lambda row: discrete_credit(row, 'credit_52278'), axis = 1)
解决方案
map
是一个 Series 方法,但您试图在标量(浮点)值上使用它。
您可以简单地执行以下操作:
df['discrete_52278'] = (
df['credit_52278']
.map({
1: 'no_credit_state',
2: 'thin_file',
3: 'no_hit'
})
.fillna('valid')
.astype('category')
)
推荐阅读
- excel - VBA VLookUp 通过数据集并将匹配的单词放在选定的单元格中
- java - 如何使用 Jetty HTTP 客户端和 MultiPartContentProvider 上传 BufferedImage?
- python - 如何删除列表中列表中的第一个元素?
- windows - 基本或高级安装模式选择以跳过或使用高级选项页面
- javascript - 从 Unity webGL 应用程序导出网格并将其与 AR.js 制造商关联
- excel - Excel VBA:加速设置列宽和视图设置的宏
- javascript - 当我使用 Redux 作为状态管理器时,我的组件没有更新?
- rest - REST API 设计:一个带有 if/else 逻辑的端点或两个独立的基于角色的端点
- java - 即使指定了 Path 变量,如何修复“CreateProcess error=2,系统找不到指定的文件”(跨平台)
- javascript - 从 Javascript 对象动态创建数组列表