【问题标题】:Filter on only two dates in pandas在熊猫中仅过滤两个日期
【发布时间】:2019-11-11 23:58:00
【问题描述】:

我有一些以日期为索引的df。我需要汇总唯一的一对日期。因此,基本上,我只需要在df 中选择两个日期,来自itertools.combinations() 函数。请注意,我不需要 范围,但我需要过滤两个确切的日期。这是我的解决方案,但它不起作用。

Dtest = ['2019-09-23', '2019-09-24']
for pair in itertools.combinations(Dtest, 2):
    print(pair)
    tframe = df[(df.iloc(pair[0]) | df.iloc(pair[1])) ]

【问题讨论】:

  • 改用lociloc 是基于位置的索引,loc 是基于标签的。
  • 但话又说回来,如果您的索引是日期时间,我不确定您是否可以用字符串索引您的数据。你能添加一个小的示例数据集,以便我们重现吗?阅读this
  • 显然2019-02-23 不在您的索引中,print('2019-09-23' in df.index) 给出了什么?
  • 很好,顺便说一句,我建议您使用df.index.isin(Dtest) 而不是循环。请参阅docs 中的底部示例

标签: python pandas datetime


【解决方案1】:

好的,所以,我找到了解决方案。我不认为它是最佳的,但它确实有效。

Dtest = ['2019-09-23', '2019-09-24']
for pair in itertools.combinations(Dtest, 2):
    tframe = pd.concat([df[pair[0]], df[pair[1]]])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    • 1970-01-01
    • 2020-05-05
    • 2013-06-09
    • 1970-01-01
    相关资源
    最近更新 更多