I’m getting Key error in python

A KeyError generally means the key doesn’t exist. So, are you sure the path key exists? From the official python docs: exception KeyError Raised when a mapping (dictionary) key is not found in the set of existing keys. For example: So, try to print the content of meta_entry and check whether path exists or not. Or, you can do:

Adding new column to existing DataFrame in Python pandas

Edit 2017 As indicated in the comments and by @Alexander, currently the best method to add the values of a Series as a new column of a DataFrame could be using assign: Edit 2015Some reported getting the SettingWithCopyWarning with this code.However, the code still runs perfectly with the current pandas version 0.16.1. The SettingWithCopyWarning aims to inform of a possibly … Read more

How can I parse a YAML file in Python

The easiest and purest method without relying on C headers is PyYaml (documentation), which can be installed via pip install pyyaml: And that’s it. A plain yaml.load() function also exists, but yaml.safe_load() should always be preferred unless you explicitly need the arbitrary object serialization/deserialization provided in order to avoid introducing the possibility for arbitrary code execution. Note the PyYaml project … Read more

Create a file if it doesn’t exist

If you don’t need atomicity you can use os module: UPDATE: As Cory Klein mentioned, on Mac OS for using os.mknod() you need a root permissions, so if you are Mac OS user, you may use open() instead of os.mknod()

List changes unexpectedly after assignment. Why is this and how can I prevent it?

With new_list = my_list, you don’t actually have two lists. The assignment just copies the reference to the list, not the actual list, so both new_list and my_list refer to the same list after the assignment. To actually copy the list, you have various possibilities: You can use the builtin list.copy() method (available since Python 3.3):new_list = old_list.copy() You can slice it:new_list … Read more

Is it possible to make abstract classes in Python?

Use the abc module to create abstract classes. Use the abstractmethod decorator to declare a method abstract, and declare a class abstract using one of three ways, depending upon your Python version. In Python 3.4 and above, you can inherit from ABC. In earlier versions of Python, you need to specify your class’s metaclass as ABCMeta. Specifying the metaclass has different … Read more