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