Python Forum

Full Version: Lamda function explanation
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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'
(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?
(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
(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