2017-05-07 72 views
2

我有DF:熊猫插入“:”入数值

CU     Parameters   01-04-2017 02-04-2017 
CU0111--H Time of Full Charge  732   726 
CU0111-016297-2 Time of Full Charge  825   815 
CU0111-020046-K Time of Full Charge  849   836 
CU0111-023156-H Time of Full Charge  922   907 
CU0111-023349-J Time of Full Charge  1121   1010 
CU0111-023350-L Time of Full Charge  1021   932 

与日期列中的值实际上是时间的日值。如何将它们转换为H:MM值,使DF1:

CU    Parameters    01-04-2017 02-04-2017 
CU0111--H Time of Full Charge  7:32  7:26 
CU0111-016297-2 Time of Full Charge  8:25  8:15 
CU0111-020046-K Time of Full Charge  8:49  8:36 
CU0111-023156-H Time of Full Charge  9:22  9:07 
CU0111-023349-J Time of Full Charge  11:21  10:10 
CU0111-023350-L Time of Full Charge  10:21  9:32 

回答

2

如果你想要的 “H:MM”:如果你想使用一个适当的

text = df['01-04-2017'].astype(str) 
df['01-04-2017'] = text.str[:-2] + ':' + text.str[-2:] 

字符串值,这样做时间D型:

hhmm = df['01-04-2017'] 
minutes = (hhmm/100).astype(int) * 60 + hhmm % 100 
df['01-04-2017'] = pd.to_timedelta(minutes, 'm') 

然后你得到这样的:

0 07:32:00 
1 08:25:00 
... 
dtype: timedelta64[ns] 

我通常更喜欢后一种方法,尤其是如果您打算在晚些时候使用这些数据。第一种基于字符串的方法只有在工作流程中的下一步需要字符串时才非常合适。

1

一个衬为所有列:

In [44]: (df.set_index(['CU','Parameters']) 
    ...: .stack() 
    ...: .astype(str) 
    ...: .str.zfill(4) 
    ...: .str.replace(r'(\d{2})(\d{2})', r'\1:\2') 
    ...: .unstack() 
    ...: .reset_index() 
    ...:) 
    ...: 
Out[44]: 
       CU   Parameters 01-04-2017 02-04-2017 
0 CU0111--H Time of Full Charge  07:32  07:26 
1 CU0111-016297-2 Time of Full Charge  08:25  08:15 
2 CU0111-020046-K Time of Full Charge  08:49  08:36 
3 CU0111-023156-H Time of Full Charge  09:22  09:07 
4 CU0111-023349-J Time of Full Charge  11:21  10:10 
5 CU0111-023350-L Time of Full Charge  10:21  09:32