- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个数据框 df1
由 datetime 索引,每分钟有一个条目,持续数周示例:
SAMPLE_TIME Bottom Top Out state
0 2015-07-15 16:41:56 48.625 55.812 43.875 1
1 2015-07-15 16:42:55 48.750 55.812 43.875 1
2 2015-07-15 16:43:55 48.937 55.812 43.875 1
3 2015-07-15 16:44:56 49.125 55.812 43.812 1
4 2015-07-15 16:45:55 49.312 55.812 43.812 1
我想找到 Avg(TempBottom,TempTop)最低的那一天,然后按分钟获取全天数据,这样我就可以绘制那一天,我试过了:
df2 = df1.groupby(pd.TimeGrouper('D')).agg(min) \
.sort(['TempTop','TempBottom'], ascending=[True,True])
这给了我订购的最低温度天数。示例:
SAMPLE_TIME Bottom Top Out state
2015-10-17 19.994 25.840 21.875 0
2015-08-29 26.182 28.777 25.937 0
2015-11-19 19.244 33.027 28.937 0
2015-11-07 19.744 33.527 28.125 0
然后我虽然我需要的只是从 df2 中获取第一个条目的索引:
df1[df2.index[1]]
但是我得到一个错误:
KeyError: Timestamp('2015-08-29 00:00:00')
最佳答案
来自 docs :
Warning
The following selection will raise a
KeyError
; otherwise this selection methodology would be inconsistent with other selection methods in pandas (as this is not a slice, nor does it resolve to one)
dft['2013-1-15 12:30:00']
To select a single row, use
.loc
In [71]: dft.loc['2013-1-15 12:30:00']
Out[71]:
A 0.193284
Name: 2013-01-15 12:30:00, dtype: float64
所以你需要在你的情况下使用 loc
方法:
In [103]: df1.loc[df2.index[0]]
Out[103]:
SAMPLE_TIME TempBottom TempTop TempOut State Bypass
2015-07-15 16:41:56 48.625 55.812 43.875 1 1
2015-07-15 16:42:55 48.750 55.812 43.875 1 1
2015-07-15 16:43:55 48.937 55.812 43.875 1 1
2015-07-15 16:44:56 49.125 55.812 43.812 1 1
2015-07-15 16:45:55 49.312 55.812 43.812 1 1
编辑
当您传递单个参数时,它会尝试使用标签进行访问。但是,当您通过间隔时,它将用作切片。您可以使用技巧来传递值 + 1 天:
In [276]: df2.index[0]
Out[276]: Timestamp('2015-07-15 00:00:00', offset='D')
In [277]: df2.index[0] + 1
Out[277]: Timestamp('2015-07-16 00:00:00', offset='D')
In [278]: df1.loc[df2.index[0]: df2.index[0] + 1]
Out[278]:
TempBottom TempTop TempOut State Bypass
SAMPLE_TIME
2015-07-15 16:41:56 48.625 55.812 43.875 1 1
2015-07-15 16:42:55 48.750 55.812 43.875 1 1
2015-07-15 16:43:55 48.937 55.812 43.875 1 1
2015-07-15 16:44:56 49.125 55.812 43.812 1 1
2015-07-15 16:45:55 49.312 55.812 43.812 1 1
EDIT2
或者您可以将 Timestamp
的 date
转换为 str
:
In [355]: df2.index[0]
Out[355]: Timestamp('2015-07-15 00:00:00', offset='D')
In [356]: df2.index[0].date()
Out[356]: datetime.date(2015, 7, 15)
In [357]: str(df2.index[0].date())
Out[357]: '2015-07-15'
In [359]: df1[str(df2.index[0].date())]
Out[359]:
TempBottom TempTop TempOut State Bypass
2015-07-15 16:41:56 48.625 55.812 43.875 1 1
2015-07-15 16:42:55 48.750 55.812 43.875 1 1
2015-07-15 16:43:55 48.937 55.812 43.875 1 1
2015-07-15 16:44:56 49.125 55.812 43.812 1 1
2015-07-15 16:45:55 49.312 55.812 43.812 1 1
关于python - 如何从一天的索引匹配中获取全天数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34207218/
我想要一些如何打印消息,例如:“从那时起,已经过去了 x 天、y 小时、z 分钟和 w 秒”。目前我正在做这样的事情,但我想念剩下的部分(最重要的是)我不喜欢它。应该有更美的东西 dt = (date
有一个令人费解的要求。 基本上我需要根据这些条件创建唯一的 id 9 位数字,当天唯一(表示如果第二天再次出现该号码也没关系) 实时生成;仅限 java(意味着没有从数据库生成序列号 - 实际上根本没
我是一名优秀的程序员,十分优秀!