【问题标题】:Get rows of dataframe whose index belongs in another dataframe's index获取索引属于另一个数据帧索引的数据帧行
【发布时间】:2018-12-07 04:07:03
【问题描述】:

如何获取数据帧 (DateTimeIndex) 中其索引属于另一个数据帧索引的所有行 (DateTimeIndex 未指定时间)?

例如,

我有一个数据框 (df1),其中包含多天的数据,但仅限于下午 2 点到 6 点的时间。我有另一个数据框(df2),其中包含多天的数据,但没有指定小时。如何访问 df1 中 df2 中的所有行?

下面是df1和df2的部分截图,

【问题讨论】:

标签: python pandas dataframe


【解决方案1】:

IIUC,你可以在索引上isin

df1.index = pd.to_datetime(df1.index)
df2.index = pd.to_datetime(df2.index)

df1 = df1.loc[df1.index.isin(df2.index),:]

或者:

df1 = df1.loc[df1.index.floor('D').isin(df2.index),:]

【讨论】:

    猜你喜欢
    • 2019-08-12
    • 2019-01-02
    • 1970-01-01
    • 1970-01-01
    • 2022-12-17
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 2022-01-24
    相关资源
    最近更新 更多