python - Applying pandas map without function
问题描述
df = pd.DataFrame({ 'A': [1,2,3,4],
'B': [10,20,30,40],
'C': [20,40,60,80]
},
index=['Row1', 'Row2', 'Row3', 'Row4'])
def mapping(row):
amap = { 1: 100, 2:200}
if row.A in amap.keys(): row.B = amap[row.A]
return row
df.apply(mapping,axis=1)
print(df)
Above code works. A B C Row1 1 100 20 Row2 2 200 40 Row3 3 30 60 Row4 4 40 80
Is there a shorter version, especially without using function? Is it worth it? Thanks.
解决方案
推荐阅读
- types - gnuplot:使用格式数据类型来分隔数字?
- angular - 可观察对象过滤可观察对象并映射到不同的可观察对象:Angular 9
- android-studio - Android Studio Widget 更新更快
- python - 如何在循环中使用 to_json 为每个键、值对创建 json,其中键是 json 文件名,值是要转换为 json 的数据帧?
- c# - 父控件绑定命令到 ContextMenu MenuItem
- c# - 如何以最佳方式在 Unity 中实例化 UI 图像的静态网格?
- r - 替换空(NA)数据框中的行而不将因子转换为整数
- node.js - Neo4jError:消息“提交”不能由处于就绪状态的会话处理
- android - 如何在 Android Studio 中解决这个问题
- c++ - 将二进制(exe、zip 等)文件读入 char*、c++