Is there a way to subtract 3 from every digit of a number?

2QdxY4RzWzUUiLuE at potatochowder.com 2QdxY4RzWzUUiLuE at potatochowder.com
Sun Feb 21 10:20:35 EST 2021


On 2021-02-21 at 09:28:39 -0500,
C W <tmrsg11 at gmail.com> wrote:

> I got it to work! I defined a separate function, and put it into
> df['col_1'].apply().
> 
> Not the most elegant, but it worked. I'm also curious how people on this
> mailing list would do it.

I don't know anything about pandas, but for one string of digits
(there's no clear requirements for how to handle leading zeros in the
input or the output), I'd end up with something like this:

OFF_BY_3_MAPPING = dict(zip('0123456789', '7890123456'))
def off_by_3(digits):
    return ''.join(OFF_BY_3_MAPPING[c] for c in digits)


More information about the Python-list mailing list