I am new to python, how to read f'{x:0>6}' in the following code
df[col].map(lambda x: f'{x:0>6}')
I found f meant f-string. But I don't understand what the whole expression mean.
It means right aligned front pad zeros for any value of x if x has less than 6 digits:
s = [(lambda x: f'{x:0>6}')(num) for num in [4, 45, 123456]]
print(s)
# ['000004', '000045', '123456']
lambda is a nameless function that is used mostly on fly: The lambda expression is equivalents to
def padd_zero(x):
return f'{x:0>6}'
s = [padd_zero(num) for num in [4, 45, 123456]]
print(s)
# ['000004', '000045', '123456']
In the example df[col].map(abc). abc is applied to all values in col, just as we apply the functions to the list above.
See: Strings Docs 3.6+
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With