Testing dictionary entries in a Pandas lambda function

Viewed 1234

When I do:

​df['ViewClass'] = df['dataset_id'].apply(
    lambda x: classdict[str(x)] if classdict[str(x)] else '???' )

This blows up if the key does not exist. How can I get get the lambda function to behave and put in '???' if the key does not exist?

I come from a Perl background and that accounts for my faulty first try here :)

3 Answers
Related