[英]How to get the first and last values from the dataframe column using a function
I need to get the first value and last values for the following using a function similar to the way I'm getting the max()
and min()
values from a Pandas DF column. 我需要使用类似于从Pandas DF列获取max()
和min()
值的函数来获取以下值的第一个值和最后一个值。 So this part of my code works perfectly to give me the max and min: 所以我的代码的这部分完美地给了我最大和最小:
day_high = day_session['High'].rolling(window=1,freq='D').max()
day_low = day_session['Low'].rolling(window=1,freq='D').min()
How do I get the first and last values respectively here using a function? 如何使用函数分别获取第一个和最后一个值?
day_open = day_session.Open.rolling(window=1,freq='D').
? ? (what is the 'first' function?) (什么是'第一'功能?)
day_close = day_session.Last.rolling(window=1,freq='D').
? ? (what is the 'last' function?) (什么是'最后'功能?)
I already tried .first()
and this gives error output ' Rolling' object has no attribute 'first'
and same for .last()
我已经尝试过.first()
并且这给出了错误输出' Rolling' object has no attribute 'first'
和.last()
相同
This is an example of the data Im using: 这是我使用的数据的示例:
data['Last']
Out[96]:
Timestamp
2013-03-07 09:30:00 1440.00
2013-03-07 09:31:00 1439.75
2013-03-07 09:32:00 1440.50
2013-03-07 09:33:00 1441.00
2013-03-07 09:34:00 1441.50
2013-03-07 09:35:00 1441.75
2013-03-07 09:36:00 1440.75
2013-03-07 09:37:00 1440.50
2013-03-07 09:38:00 1440.75
2013-03-07 09:39:00 1440.00
This code is used for the rolling window and works fine for the max() Min() above: 此代码用于滚动窗口,适用于上面的max()Min():
daystart = '9:30'
dayend = '16:14:59'
day_session = data.between_time(daystart,dayend, include_start=True, include_end=True)
I am using concat to utilise this data with other series data in a df called all_vals
我使用concat将这些数据与名为all_vals
的df中的其他系列数据all_vals
all_vals = pd.concat([day_open,day_close,day_high,day_low,day_range,IB_high,IB_low,IB_Range,upper_quartile,lower_quartile,end_session_high,end_session_low], axis=1)
thanks 谢谢
select the first and last value of a particular column of a data frame 选择数据框的特定列的第一个和最后一个值
df['column_name'].iloc[0] # first element
df['column_name'].iloc[-1] # last element
Have you tried to resample using ohlc
? 你试过用ohlc
重新采样吗?
df = pd.DataFrame(
{'price': [100, 101, 102, 103]},
index=pd.to_datetime(['2016-07-08 10:00', '2016-07-08 10:00', '2016-07-08 10:00', '2016-07-08 10:00']))
>>> df.resample('D', how={'price': 'ohlc'})
price
open high low close
2016-07-08 100 103 100 103
You should generally post questions with some sample data and expected results. 您通常应该使用一些示例数据和预期结果发布问题。
EDIT Given your sample data, you can do this: 编辑根据您的样本数据,您可以这样做:
df.resample('D', how={'Last': 'ohlc'})
Last
open high low close
Timestamp
2013-03-07 1440 1441.75 1439.75 1440
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.