Python Forum
Lamda function explanation - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Lamda function explanation (/thread-11342.html)



Lamda function explanation - mrcool4 - Jul-04-2018

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


RE: Lamda function explanation - buran - Jul-04-2018

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'


RE: Lamda function explanation - mrcool4 - Jul-04-2018

(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?


RE: Lamda function explanation - buran - Jul-04-2018

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


RE: Lamda function explanation - mrcool4 - Jul-04-2018

(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.html#format-string-syntax
Format Specification Mini-Language can be used not only with str.format() but with f-strings in 3.6+ too

Thank you.. Smile