python - 将字典项目映射到熊猫系列时忽略大小写
问题描述
我有一本包含大约 10 个数据框的字典。键是数据框名称
dataFramesDict[sheet_1] = pd.DataFrame({'Date':['2007-05-30','2107-11-30','2207-05-20','2307-05-20'],'Value': [2.4,2.5,2.6,2.7],'Test': ['Height','Weight','Systolic Blood Pressure Measurement','Diastolic Blood Pressure Measurement']})
我要做的是创建一个名为unit
但通过将其映射到熊猫系列 ( unit_dict
) 的新列,这是一个熊猫系列,如下所示。Term
是索引名称。
我试图避免区分大小写或降低两者。
下面的代码抛出错误
def add_units():
for k in dataFramesDict.keys():
dataFramesDict[k]['unit'] = dataFramesDict[k]['Test'].str.lower().map(unit_dict.index.str.lower())
print("units are added to the measurements successfully")
解决方案
用于torename
的str.lower
索引:Series
lowercase
unit_dict = unit_dict.rename(str.lower)
dataFramesDict[k]['unit'] = dataFramesDict[k]['Test'].str.lower().map(unit_dict)
#alternative
unit_dict.index = unit_dict.index.str.lower()
dataFramesDict[k]['unit'] = dataFramesDict[k]['Test'].str.lower().map(unit_dict)
推荐阅读
- python - 我怎样才能把它放在一个循环中,在开始时尝试使用一个while循环,但我会得到错误的输出
- apache-spark - 在写入雪花之前是否必须将胶水动态帧转换为火花数据帧?
- wordpress - 用于 wordpress 日志的 logrotation
- java - 从二维数组或矩阵中删除重复项
- oracle - 如何在 Oracle ADW 中上传带有西里尔文的数据?
- c++ - 抛出错误异常:读取访问冲突
- macos - 在 MAC 上找不到 VScode 实时服务器扩展
- c++ - “类不存在默认构造函数”即使我有构造函数?
- python - Python模块中全局变量中使用的补丁方法
- node.js - 如何在 node-api 和 angular 两端加密-解密有效负载/数据?