简体   繁体   English

如何使用函数从dataframe列获取第一个和最后一个值

[英]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.

相关问题 如何使用 Python Pandas 在 dataframe 的最后一列中获取第一个具有货币格式的单元格的行索引 - How to get the row index of the first cell with currency formatting in the last column of a dataframe using Python Pandas 如何使用 function 替换 dataframe 列值 - How to replace dataframe column values using a function 如何使用数据框中的值获取列名? - How to get the column name using values in a dataframe? 如何 select 从最后一行到第一行的列名具有有效值? - How to select the column name from the last and the first row with valid values? 如何根据 Pandas 中第一个数据框中的某些列值从第二个数据框中添加列值 - How to add Column Values from 2nd DataFrame Based on Some Column Values in First dataframe in Pandas 如何使用 .loc[] 访问数据框的最后一列? - how to get access of last column of a dataframe by using .loc[]? 如何从熊猫数据框中获取最后 15 个值的总和 - How to get the sum of the last 15 values from a pandas dataframe 根据列中的值获取数据框组的第一行和最后一行 - Getting first and last rows of dataframe groups based on values in a column 如何在Pandas数据框列中打印真实值的单独组的第一个和最后一个索引 - How to print the first and last indices of separate groups of true values in a Pandas dataframe column 如何根据第一个数据帧列中的值从另一个数据帧添加新列? - How to add new column from another dataframe based on values in column of first dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM