Pandas Dataframe:如何按索引选择一行,然后获取接下来的几行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48911531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas Dataframe: How to select a row by index, and then get the next few rows
提问by Toby Mao
In pandas, I have a Dataframe indexed by timestamp. Looks like the following table:
在Pandas中,我有一个按时间戳索引的数据帧。看起来像下表:
A B C D
DATE
2018-01-17 157.52 163.74 157.28 159.84
2018-01-16 158.25 159.35 155.93 157.40
2018-01-15 157.15 159.59 156.79 158.86
2018-01-12 158.25 158.62 157.40 157.52
And I'm trying to select a row by index, and also select the next few rows. (For example, select two rows start at 2018-01-12). I found both .loc and .iloc are hard to do such task. Is there any other ways to do that?
我正在尝试按索引选择一行,并选择接下来的几行。(例如,选择从 2018-01-12 开始的两行)。我发现 .loc 和 .iloc 都很难完成这样的任务。有没有其他方法可以做到这一点?
回答by Yaniv
Solution #1: Using the DataFrame's index, followed by head(2)
:
解决方案#1:使用 DataFrame 的索引,然后是head(2)
:
df['2018-01-12':].head(2)
Solution #2: Using iloc
:
解决方案#2:使用iloc
:
i = df.index.get_loc('2018-01-12')
df.iloc[i:i+2]
Bonus solution #3: It seems like you're analyzing stock data. Maybe you're interested in something that could be more efficiently done using rolling windows? (Moving Averages maybe?) Consider using rolling
, e.g. to calculate the rolling mean:
奖励解决方案#3:您似乎正在分析股票数据。也许您对使用滚动窗口可以更有效地完成的事情感兴趣?(可能是移动平均线?)考虑使用rolling
,例如计算滚动平均值:
df.rolling(2).mean()
回答by LangeHaare
You should be able to do something like:
您应该能够执行以下操作:
date_of_wanted_row = "2018-01-12"
iloc_of_wanted_row = df.index.get_loc(date_of_wanted_row)
return df.iloc[iloc_of_wanted_row: iloc_of_wanted_row + 4]
(but with more sensible variable names, and I'm assuming the dates in the index are not really strings)
(但有更合理的变量名,我假设索引中的日期不是真正的字符串)
回答by Austin
回答by vstram
You can use (to get values between dates):
您可以使用(获取日期之间的值):
df.loc['2018-01-12':'2018-01-14']
or, to get all data from January:
或者,要获取 1 月份的所有数据:
df.loc['2018-01']