python - 用 dict 替换数据框行
问题描述
我有来自数据框的以下行:
print(row) =
a Nan
b NaN
c NaN
d NaN
e NaN
我有一个字典:dict1 = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
我想用它替换这一行。
我尝试了熊猫系列替换row = row.replace(dict1, regex=True)
,但它不起作用。我怎样才能做到这一点?
解决方案
采用:
s = pd.Series(row.index.map(dict1.get), index=row.index)
print (s)
a 1
b 2
c 3
d 4
e 5
dtype: int64
如果只想替换索引:
row.index = row.index.map(dict1.get)
推荐阅读
- java - Spring 4,没有 Spring Boot 的 JUnit 4 NoSuchBeanDefinitionException:没有符合条件的 bean 类型
- google-cloud-platform - 虚拟机实例不使用谷歌云 dns 转发器来解析
- python - 将 pd.series 的分钟/秒添加到固定的日期时间 pandas
- javascript - 在静态模板中,脚本标签导入另一个文件不起作用
- javascript - 如何检测当前页面是否为 AMP?
- laravel - Laravel Eloquent 关系方法语法
- embedded - CAN中的错误主动节点和错误被动节点有什么区别?
- python - 有没有办法通过命令发送嵌入图片?
- php - PHP:在没有作曲家的情况下合并 PDF?
- r - 带有两个向量的粘贴 + 斜体