如果我有一个按周日期(2019-01-07、2019-01-14、2019-01-21...等)索引的数据框,在 Pandas 中是否有一种方法可以有效地仅选择对应的行到指数中每个月的最后一周?
最佳答案
只是 get一个月的最后一天( MonthEnd
),然后进行过滤,例如(假设您的 Date
列中有 DataFrame
):
from pandas.tseries.offsets import MonthEnd
df['MonthEnd'] = df['Date'] + MonthEnd(1)
df[ (df['MonthEnd'] - df['Date']).dt.days <= 7 ]
关于python - 仅从数据框中选择每个月的最后一周 - Python/Pandas,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61063822/