I was fiddling around with plydata, and I wanted to map some non-categorial data columns. For plotine, I often have the problem that I have to rename values to get proper labels. With dplyr, there is dplyr::mapvalues(), which is was missing in plydata. My quick hack for that looks like this:
def mapvalues(column, keys=None, values=None, na_action=None, **kwargs):
translate_dict = None
if keys is not None:
assert values is not None and len(keys) == len(values)
translate_dict = dict(zip(keys, values))
else:
assert kwargs is not None
translate_dict = kwargs
def mapper(df):
return df[column].map(translate_dict, na_action=na_action)
return mapper
I was fiddling around with plydata, and I wanted to map some non-categorial data columns. For plotine, I often have the problem that I have to rename values to get proper labels. With dplyr, there is dplyr::mapvalues(), which is was missing in plydata. My quick hack for that looks like this:
Usage:
If there is a better way or a package that already provides this, just let me know. Otherwise, I think it would be a good addition to plydata.