我正在嘗試使用pd.merge_asof.
它們都包含 2 個以日期時間為索引列的列和一個具有浮動值的變數列。它們的索引和時間不平衡,所以我必須標準化這些值。
Date value1
2021-10-22 19:22:25 23.5
2021-10-22 19:22:40 23.4
2021-10-22 19:22:55 23.5
2021-10-22 19:30:12 23.6
2021-10-22 19:30:42 23.5
Date value2
2021-10-22 19:22:25 12
2021-10-22 19:22:40 12
2021-10-22 19:22:55 12
2021-10-22 19:30:12 16
2021-10-22 19:30:42 16
我可以成功地合并 dfs 并使用這樣的規范化值
merged = pd.merge_asof(data_frames[0],data_frames[1], left_index=True,right_index=True,direction='nearest')
Date value1 value2
2021-10-22 19:22:25 23.5 12
2021-10-22 19:22:40 23.4 12
2021-10-22 19:22:55 23.5 12
2021-10-22 19:30:12 23.6 16
2021-10-22 19:30:42 23.5 16
現在我想做的是合并兩個以上的資料框。我試過這樣做:
merged = pd.merge_asof(data_frames[0],data_frames[1],data_frames[2],left_index=True,right_index=True,direction='nearest')
但我得到了錯誤
pandas.errors.MergeError: Can only pass argument "on" OR "left_index" and "right_index", not a combination of both.
我不確定它表示什么。我洗掉了索引引數之一,它仍然說了同樣的話。有什么辦法可以得到我需要做的嗎?
我希望能夠將具有 value3 列的 dataframe3 附加到 value2 列的右側。
uj5u.com熱心網友回復:
根據檔案,merge_asof只能接受兩個資料幀,導致錯誤是因為在函式的第三個引數中它需要一些其他引數。正如@Quang Hoang 提到的,您可以使用該reduce函式累積地應用兩個引數函式。你的情況是:
merged = reduce(lambda left, right: pd.merge_asof(left, right,left_index=True,right_index=True, direction='nearest'), data_frames)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/458751.html
上一篇:如何從特定范圍按月和年分組?
下一篇:將大資料框拆分為更小的子集列
