简体   繁体   English

熊猫-使用列作为字典的键

[英]pandas - using a column as a key for a dictionary

One of the columns " Status " in my dataframe dfUnderInterpretation has values like " OK ", " Missing " and " New ". 我的数据框dfUnderInterpretation中的状态 ”列之一具有诸如“ 确定 ”,“ 缺失 ”和“ 新建 ”之类的值。

Another variable statusInterpretation is a dict: {'OK': 'INFO', 'New': 'WARN', 'Missing': 'ERROR' } 另一个变量statusInterpretation是一个dict:{'OK':'INFO','New':'WARN','Missing':'ERROR'}

I want to create a new column ' Interpretation ' with dict values based on index in " Status " column of my dataframe. 我想根据我的数据框“ 状态 ”列中的索引创建一个包含解释值的新列“ 解释 ”。

This below of course wont work because dfUnderInterpretation [' Status '] is a list. 由于dfUnderInterpretation [' Status ']是一个列表,因此下面这当然不起作用。

dfUnderInterpretation['Interpretation'] = statusInterpretation.get(dfUnderInterpretation['Status'], None) dfUnderInterpretation ['Interpretation'] = statusInterpretation.get(dfUnderInterpretation ['Status'],无)

您可以使用apply方法:

dfUnderInterpretation['Status_Desc'] = dfUnderInterpretation['Status'].apply(lambda x: statusInterpretation.get(x))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM