I'm trying to add a new column to a DataFrame based on the boolean values in another column.
Given a DataFrame like this:
snr = DataFrame({ 'name': ['A', 'B', 'C', 'D', 'E'], 'seniority': [False, False, False, True, False] })
The furthest I've come so far is this:
def refine_seniority(contact):
contact['refined_seniority'] = 'Senior' if contact['seniority'] else 'Non-Senior'
snr.apply(refine_seniority)
yet I'm getting this error:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-208-0694ebf79a50> in <module>()
2 contact['refined_seniority'] = 'Senior' if contact['seniority'] else 'Non-Senior'
3
----> 4 snr.apply(refine_seniority)
5
6 snr
/usr/lib/python2.7/dist-packages/pandas/core/frame.pyc in apply(self, func, axis, broadcast, raw, args, **kwds )
4414 return self._apply_raw(f, axis)
4415 else:
-> 4416 return self._apply_standard(f, axis)
4417 else:
4418 return self._apply_broadcast(f, axis)
/usr/lib/python2.7/dist-packages/pandas/core/frame.pyc in _apply_standard(self, func, axis, ignore_failures)
4489 # no k defined yet
4490 pass
-> 4491 raise e
4492
4493
KeyError: ('seniority', u'occurred at index name')
Feels like I'm missing some fundamental understanding on DataFrames, but I'm stuck.
What's the proper way to add a new column based on boolean values in a different column?