简体   繁体   中英

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> . Then perform a simple 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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