Pandas将“:”插入数值

时间:2017-05-07 10:22:23

标签: python pandas

我有df:

CU                 Parameters         01-04-2017    02-04-2017
CU0111-012379-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-012379-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 个答案:

答案 0 :(得分:2)

如果你想要" h:mm"字符串值,执行以下操作:

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

如果你想使用适当的时间dtype:

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 :(得分: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-012379-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