簡體   English   中英

如何將較大的 pandas Dataframe 的日期時間索引復制到長度不等的較小 Dataframe 的日期時間索引?

[英]How to copy datetime index of a bigger pandas Dataframe to a smaller Dataframe with unequal length?

我有一個帶有日期時間索引的 dataframe

2021-05-05    1923.275024
2021-05-06    1920.799988
2021-05-07    1940.825012
2021-05-10    1933.875000
2021-05-11    1924.275024
2021-05-12    1919.950012
2021-05-14    1924.000000

我有另一個 dataframe 與 Integer 索引

0      0.342828
1      0.791374
2      1.261493
3      1.725175
4      2.114718

我希望我的第二個 dataframe 的索引與第一個相似,但由 2 個日期偏移。

期望的結果:`

    2021-05-07      0.342828
    2021-05-10      0.791374
    2021-05-11      1.261493
    2021-05-12      1.725175
    2021-05-14      2.114718

我認為我不能使用datetime.timedelta和 shift 因為日期不連續。 提前致謝

使用set_index

df2 = df2.set_index(df1[-len(df2):].index)
print(df2)

# Output
2021-05-07  0.342828
2021-05-10  0.791374
2021-05-11  1.261493
2021-05-12  1.725175
2021-05-14  2.114718

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM