[英]Look up from a dictionary to fill pandas DataFrame with a condition
I have a dictionary that's something like this.我有一本像这样的字典。
d = {
"p1": ["$0.00", nan, "$25.00"],
"p2": ["$30.25", nan, "$12.25"],
"p3": ["$0.00"]
}
First rework your dictionary to keep the "highest" value with a custom key<\/code> .
首先修改您的字典以使用自定义
key<\/code>保持“最高”值。
Then perform a simple
map<\/code> :
然后执行一个简单的
map<\/code> :
d2 = {k: max(v, key=lambda x: float(x.strip('$')) if isinstance(x, str) else x)
for k,v in d.items()}
# {'p1': '$25.00', 'p2': '$30.25', 'p3': '$0.00'}
df['val'] = df['name'].map(d2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.