python - 将 DateTimeIndex 的时间元素分配给新列

标签 python pandas dataframe datetimeindex

我正在使用下面的DataFrame并且只想访问我的DateTimeIndex的时间(而不是日期):

                    idle wheel  Induce wheel axial  radial
tiempo          
5/30/2016 19:37:46  -1,099.12   -1,048.78   -477.13
5/30/2016 19:37:47  -1,099.12   -1,048.78   -476.98
5/30/2016 19:37:48  -1,099.12   -1,048.78   -477.21
5/30/2016 19:37:49  -1,099.12   -1,048.78   -477.13
5/30/2016 19:37:50  -1,099.12   -1,048.78   -477.21
5/30/2016 19:37:51  -1,099.12   -1,048.78   -477.35
5/30/2016 19:37:52  -1,099.12   -1,048.78   -477.13
5/30/2016 19:37:53  -1,099.12   -1,048.78   -476.98
5/30/2016 19:37:54  -1,099.12   -1,048.78   -476.98
5/30/2016 19:37:55  -1,099.12   -1,048.78   -476.98
5/30/2016 19:37:56  -1,099.12   -1,048.78   -476.98
5/30/2016 19:37:57  -1,099.12   -1,048.78   -476.98

我只想保留 19:..:..,而不是日期部分。 我一直在寻找但找不到任何解决方案。

最佳答案

在您的索引上使用.time 'tiempo':

df['time'] = df.index.time

或者,如果'tiempo'使用

df['time'] = df.tiempo.dt.time

获取:

                tiempo  idle wheel  Induce wheel axial    radial      time
0  2016-05-30 19:37:46    -1099.12    -1048.78           -477.13  19:37:46
1  2016-05-30 19:37:47    -1099.12    -1048.78           -476.98  19:37:47
2  2016-05-30 19:37:48    -1099.12    -1048.78           -477.21  19:37:48
3  2016-05-30 19:37:49    -1099.12    -1048.78           -477.13  19:37:49
4  2016-05-30 19:37:50    -1099.12    -1048.78           -477.21  19:37:50
5  2016-05-30 19:37:51    -1099.12    -1048.78           -477.35  19:37:51
6  2016-05-30 19:37:52    -1099.12    -1048.78           -477.13  19:37:52
7  2016-05-30 19:37:53    -1099.12    -1048.78           -476.98  19:37:53
8  2016-05-30 19:37:54    -1099.12    -1048.78           -476.98  19:37:54
9  2016-05-30 19:37:55    -1099.12    -1048.78           -476.98  19:37:55
10 2016-05-30 19:37:56    -1099.12    -1048.78           -476.98  19:37:56
11 2016-05-30 19:37:57    -1099.12    -1048.78           -476.98  19:37:57

关于python - 将 DateTimeIndex 的时间元素分配给新列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37572827/

相关文章:

python - Pandas 中的滚动减法

python - 用 pandas 解析 CSV 并转换为 dict 结果仅在最后一行出现 KeyError

python - Pandas 两个分组的 DataFrame 列的简单关联

python - 将新列数组添加到 Pandas 数据框中

reshape 数据框 - 更改列和行

python - 我在实例化期间传递给类的参数/kwargs __init__ 被忽略

python - 关闭 QMainWindow 的正确方法

python - 将两个整数列加在一起,避免字符串和 Nan

python - 如何使用python在excel单元格中的(...)内获取整数值并将其存储到变量中

python - Pandas 到 Excel(合并标题列)