Mike, You moved the goalpost.
Some of us here have been speculating you were asking what we call a homework question here. The problem seemed to be the kind asked for that can be done using fairly simple commands in python combined together. Of course, some of the answers posted used ideas usually not taught early and that had to be explained. The one you liked uses anonymous functions handed to a function that repeatedly applies it to a list of items created from taking a number apart after it is I string form and then recombines it. My method is straightforward enough and similar to another posting because it just follows the logic. >>> start = 1234567890987654321 >>> int(''.join([str((int(dig)+3) % 10) for dig in str(start)])) 4567890123210987654 But you just moved the goalpost by talking about using a data.frame as that (and I assume numpy and pandas) are not very basic Python. So think about it. You have a column in a data.frame. How do you apply other transformations to it? In one sense, a column in a data.frame is just one of many kinds of ordered collects in Python, not much different than a list. How do you apply a set of instructions to take in one collection and replace it with another computed item by item? Since you know about making your own functions, you know that a clean way of doing many things is to encapsulate the gory details in a function and call it the usual way to apply the transformation. Sure, you could use another anonymous function and make it messier to read. And, if you know of the capabilities of a data.frame structure, you use the method that it already offers and hand it your function in one of multiple ways. Can you satisfy our curiosity about what this functionality is for, now that it seems you may have a bigger purpose? Or, is it HW for a more advanced class? -----Original Message----- From: Python-list <python-list-bounces+avigross=verizon....@python.org> On Behalf Of C W Sent: Sunday, February 21, 2021 9:21 AM To: Python <python-list@python.org> Subject: Re: Is there a way to subtract 3 from every digit of a number? I do want to follow up, if I may. In Ming's example, a = 2342 b = int("".join(map(lambda x: str((int(x)-3)%10) ,list(str(a))))) What's the best approach to apply to a dataframe column, rather than just one value? Here's my attempt using df[col_1''].apply(), df['col_1'].apply(lambda:a int("".join(map(lambda x: str((int(x)-3)%10) ,list(str(a)))))) Thanks! On Sun, Feb 21, 2021 at 9:12 AM C W <tmrs...@gmail.com> wrote: > Thanks so much everyone, I appreciate it! > > Ming, your solution is awesome. More importantly, very clear > explanations on how and why. So, I appreciate that. > > Thanks again, cheers! > > Mike > > On Sun, Feb 21, 2021 at 12:08 AM <2qdxy4rzwzuui...@potatochowder.com> > wrote: > >> On 2021-02-20 at 20:49:15 -0800, >> Dan Stromberg <drsali...@gmail.com> wrote: >> >> > On Sat, Feb 20, 2021 at 7:13 PM Ming <m...@pgp.cool> wrote: >> > >> > > I just wrote a very short code can fulfill your needs: >> > > >> > > a = 2342 >> > > b = int("".join(map(lambda x: str((int(x)-3)%10) ,list(str(a))))) >> > > >> > I tend to favor plenty of temporary variables with descriptive >> > names, >> but >> > this is indeed short. >> > >> > Apart from that, you may find that using a generator expression is >> shorter >> > and clearer than map+lambda. It should allow to additionally >> > eliminate >> the >> > list conversion. >> > >> > So in the terse form you've got there, it'd be more like: >> > b = int(''.join(str((int(x) - 3) % 10) for x in str(a)) >> > >> > But in real life, I'd try to use descriptive variable names for >> > some of >> the >> > subexpressions in that. This makes reading and debugging simpler, >> which is >> > important because the maintenance phase of software is almost >> > always >> much >> > longer and costly than the development phase. And although you >> > could >> do a >> > generator expression for each of the different parts of (int(x) - >> > 3) % >> 10, >> > I kinda like having a named function for just that piece. >> > >> > So maybe: >> > def rot_3(character): >> > """Convert to int, subtract 3 and mod 10.""" >> > digit = int(character) >> > assert 0 <= digit <= 9 >> > return (digit - 3) % 10 >> > >> > >> > def descriptive_minus_three_caesar(input_number): >> > """Convert to a -3 caesar cypher on an integer.""" >> > string_number = str(input_number) >> > rotated_digits = (rot_3(character) for character in string_number) >> > output_string = ''.join(str(digit) for digit in rotated_digits) >> > output_number = int(output_string) >> > return output_number >> >> >>> descriptive_minus_three_caesar('38') >> 5 >> >> The problem is underspecified, and the examples are lacking, but >> based on the phrase "each digit" and the examples that contain a 3, >> I'd prefer to see "38" become "05." >> -- >> https://mail.python.org/mailman/listinfo/python-list >> > -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list