将Pandas DatetimeIndex与条件较小的DatetimeIndex进行比较 [英] Compare Pandas DatetimeIndex to a smaller DatetimeIndex with condition

查看:70
本文介绍了将Pandas DatetimeIndex与条件较小的DatetimeIndex进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试比较两个具有不同长度的Pandas DatetineIndexs.我只想在我的较大的Dataframe(df)中添加一个新列,然后在与较小的DatetineIndex(事件)匹配的地方放置1.

I am trying to compare two Pandas DatetineIndexs with different lengths. I want to simply add a new column to my larger Dataframe (df) and place 1 where it matches the smaller DatetineIndex (events).

我尝试了

df['filter'] = np.where(df.index == tEvents, 1, np.nan)

但是我收到"ValueError:长度必须匹配才能进行比较"

but I get "ValueError: Lengths must match to compare"

我被困在这里的时间比我承认的时间长

I've been stuck here longer than I like to admit

推荐答案

tEvents df.index 的长度不同. df.index == tEvents 看起来可以比较两个列表.

tEvents and df.index are different lengths. df.index == tEvents looks to compare the two lists.

您要检查 df.index 中的元素是否在 tEvents 中.因此,将 df.index == tEvents 替换为 df.index.isin(tEvents)

You want to check if an element in df.index is in tEvents. Thus replace df.index == tEvents with df.index.isin(tEvents)

要查看如果日期匹配则添加True或false值,请使用 DataFrame.isin()

To see add a True or false value if date matches, use DataFrame.isin()

这篇关于将Pandas DatetimeIndex与条件较小的DatetimeIndex进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆