Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Lamda function explanation
#1
Hi All,

Can someone please explain the below code?I am mainly interested in the lambda function lambda x: '{0:0>10}'.format(x). Thanks.
## Add leading zeros to the integer column in Python
 
df['Col1']=df['Col1'].apply(lambda x: '{0:0>10}'.format(x))
print df
TIA
Reply
#2
lambda x: '{0:0>10}'.format(x) creates a string that has length at least 10. if what you pass as parameter has length less than 10 it will left-fill it with 0s.
e.g. if you have value of 1 in Col1, it will become '0000000001'
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Jul-04-2018, 10:10 AM)buran Wrote: lambda x: '{0:0>10}'.format(x) creates a string that has length at least 10. if what you pass as parameter has length less than 10 it will left-fill it with 0s.
e.g. if you have value of 1 in Col1, it will become '0000000001'

Thanks Mate. How does the following code {0:0>10} actually work? Is there any documentation on this syntax?
Reply
#4
(Jul-04-2018, 10:15 AM)mrcool4 Wrote: Is there any documentation on this syntax?
https://docs.python.org/3/library/string...ing-syntax
Format Specification Mini-Language can be used not only with str.format() but with f-strings in 3.6+ too
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(Jul-04-2018, 10:22 AM)buran Wrote:
(Jul-04-2018, 10:15 AM)mrcool4 Wrote: Is there any documentation on this syntax?
https://docs.python.org/3/library/string...ing-syntax
Format Specification Mini-Language can be used not only with str.format() but with f-strings in 3.6+ too

Thank you.. Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Explanation of code ejKDE 4 305 Feb-26-2024, 02:50 PM
Last Post: ejKDE
  A better explanation of the last post Led_Zeppelin 9 2,321 Sep-20-2022, 05:08 PM
Last Post: deanhystad
  Changing a string value to a numerical value using python code and a lamda function Led_Zeppelin 6 1,538 Jul-05-2022, 11:29 PM
Last Post: deanhystad
  Operator meaning explanation Sherine 3 1,980 Jul-31-2021, 11:05 AM
Last Post: Sherine
  Explanation of except ... as : Fernando_7obink 2 1,884 Feb-13-2021, 04:45 AM
Last Post: deanhystad
  .maketrans() - a piece of code which needs some explanation InputOutput007 5 2,898 Jan-28-2021, 05:05 PM
Last Post: buran
  .remove() from a list - request for explanation InputOutput007 3 2,177 Jan-28-2021, 04:21 PM
Last Post: InputOutput007
  Explanation of the left side of this statement please rascalsailor 3 2,451 Sep-09-2020, 02:02 PM
Last Post: rascalsailor
  Need explanation of one line of code Fliberty 6 3,409 Feb-18-2020, 12:50 AM
Last Post: Fliberty
  explanation of code hikerguy62 2 2,217 Aug-01-2019, 01:37 PM
Last Post: hikerguy62

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020