简体   繁体   English

如何将前导零填充到“时间”列?

[英]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'] = df_dropped['Time'].apply(lambda x:'{:0>4}'.format(x))

I don't understand what the '{:0>4}'.format(x)' does. 我不明白'{:0>4}'.format(x)'作用。 Please explain the construction of this line '{:0>4}'.format(x)' 请解释此行'{:0>4}'.format(x)'的构造

It adds 0 character to each elements of data-frame until it reach 4 character. 它将0个字符添加到数据帧的每个元素,直到达到4个字符。 If the element is more than 4 character will do nothing. 如果元素大于4个字符,则不执行任何操作。 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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM