How to pad leading zeros to the Time column?

Using 'df_dropped', a data frame, which has a column 'Time'.

df_dropped['Time'] = df_dropped['Time'].apply(lambda x:'{:0>4}'.format(x))

I don't understand what the '{:0>4}'.format(x)' does. Please explain the construction of this line '{:0>4}'.format(x)'

Upvotes: 0

Views: 182

Answers (1)

Saeed Heidari
Saeed Heidari

Reputation: 419

It adds 0 character to each elements of data-frame until it reach 4 character. If the element is more than 4 character will do nothing. you can see below example:

import pandas as pd

df = pd.DataFrame(data=[23, "fsda", 289801, 87], columns=['Time'], index=[0, 1, 2, 3])
df['Time'] = df['Time'].apply(lambda x: '{:0>6}'.format(x))

Upvotes: 2

Related Questions