[英]How to slice the Time in Hours and Minutes in Pandas DataFrame
I am trying to split the Time column from my dataset.我正在尝试从我的数据集中拆分时间列。 The Time column has a value like this '2324' instead of '23:24'.
Time 列的值类似于“2324”而不是“23:24”。 I have used this command df['MINUTES']=df['MINUTES'].str[1:3].
我使用了这个命令 df['MINUTES']=df['MINUTES'].str[1:3]。 but it didn't work accurately, since the time column is based on 24 hours.
但它不能准确地工作,因为时间列是基于 24 小时的。 So '2324' showing as '23:32' which is incorrect.How do I split them into proper way.
所以'2324'显示为'23:32'这是不正确的。我如何将它们分成正确的方式。 Please be gentle I am just starting out in Python/DA field.
请温柔,我刚开始涉足 Python/DA 领域。
Thanks in advance!提前致谢! Anil
阿尼尔
I am not sure where did the issue arise, since having 24 hrs time shouldn't affect the script.我不确定问题出在哪里,因为有 24 小时的时间不应该影响脚本。 Here's an example that seems to match the expected output:
这是一个似乎与预期的 output 匹配的示例:
import pandas as pd
df = pd.DataFrame({'Example':['1242','1342','1532','1643','1758','1821','1902','0004','2324']})
df['Hour'] = df['Example'].str[:2]
df['Minute'] = df['Example'].str[2:]
df['Time'] = df['Example'].str[:2] + ":" + df['Example'].str[2:]
This generates the following output:这将生成以下 output:
Example Hour Minute Time
0 1242 12 42 12:42
1 1342 13 42 13:42
2 1532 15 32 15:32
3 1643 16 43 16:43
4 1758 17 58 17:58
5 1821 18 21 18:21
6 1902 19 02 19:02
7 0004 00 04 00:04
8 2324 23 24 23:24
Here is what you can do:这是您可以执行的操作:
df['MINUTES'].replace(['(?<=\d\d)(?=\d\d)'], ':', regex=True, inplace=True)
We are basically telling python to inset a colon ':'
in this gap: '(?<=\d\d)(?=\d\d)'
, which is between two digits on each side.我们基本上是在告诉 python 在这个间隙中插入一个冒号
':'
: '(?<=\d\d)(?=\d\d)'
,它在每边的两位数之间。
Lets test it:让我们测试一下:
import pandas as pd
df = pd.DataFrame({'MINUTES':['1234',
'7654',
'8766']})
df['MINUTES'].replace(['(?<=\d\d)(?=\d\d)'], ':',
regex=True,
inplace=True)
print(df)
Output: Output:
MINUTES
0 12:34
1 76:54
2 87:66
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.