簡體   English   中英

熊貓數據幀..逐行重采樣

[英]Pandas dataframe .. resampling row by row

我有一些當日庫存數據(例如,DateTime,Open,Close),我想通過當天的總計測量來擴展。 像:日期時間,打開時間,關閉時間,t_Open(打開日期),t_Close(關閉日期)。

當我使用resample()時,它會整天重新采樣:

               DateTime     Open    Close   T_OPEN  T_CLOSE
165 2017-04-26 08:00:00  12485.6  12473.6  12485.6  12463.4
166 2017-04-26 08:15:00  12473.9  12473.9  12485.6  12463.4
167 2017-04-26 08:30:00  12473.6  12466.1  12485.6  12463.4
168 2017-04-26 08:45:00  12466.4  12469.6  12485.6  12463.4
169 2017-04-26 09:00:00  12470.1  12460.4  12485.6  12463.4
170 2017-04-26 09:15:00  12460.1  12460.1  12485.6  12463.4
171 2017-04-26 09:30:00  12459.9  12459.6  12485.6  12463.4
172 2017-04-26 09:45:00  12459.4  12463.4  12485.6  12463.4
173 2017-04-26 10:00:00  12462.9  12463.4  12485.6  12463.4

但是我正在尋找可以逐行重新采樣的東西。 因此,每一行的收盤價等於t_close,因為它是在給定點的最后一個obs(並且t_High將是該行的最高最高價)。

像這樣:

               DateTime     Open    Close   T_OPEN  T_CLOSE
165 2017-04-26 08:00:00  12485.6  12473.6  12485.6  12473.6
166 2017-04-26 08:15:00  12473.9  12473.9  12485.6  12473.9
167 2017-04-26 08:30:00  12473.6  12466.1  12485.6  12466.1
168 2017-04-26 08:45:00  12466.4  12469.6  12485.6  12469.6

你有想法嗎?

更新在評論之后,我決定創建一個更通用的問題:)我需要根據所有數據計算其他列, 直到我要處理的行為 這意味着:

T_OPEN = is always equal to the "OPEN" value from the first Obs of that day
T_Close = is always the "CLOSE" value from the current line 
T_HIGH = is the highest "HIGH" value until that row of that day
T_LOW = is the lowest "LOW" value until that row of that day

我可以通過對該數據幀進行兩次迭代來解決此問題...但是我一直在尋找/希望找到更多類似pandaslike的選項

感謝和最良好的祝願,E。

我想我明白了您要做什么。 這應該工作:

df = df.loc[df['Open'] <= df['Close']]
df['T_CLOSE'] = df['Close']

我已經通過迭代和保留較高/較低值中的值解決了該問題

謝謝! Elenio

暫無
暫無

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

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