pandas 从指定的行号遍历行 [英] Pandas Iterate through rows from specified row number
问题描述
我想通过遍历从特定行号开始的行来从pandas数据框中读取数据.我知道有 df.iterrows()
,但它不允许我指定要从何处开始迭代.
I want to read data from a pandas dataframe by iterating through the rows starting from a specific row number. I know there's df.iterrows()
, but it doesn't let me specify from where I want to start iterating.
在我的特定情况下,我有一个csv文件,看起来可能像这样:
In my specific case, I have a csv file that might look something like this:
Date, Temperature
21/08/2017 17:00:00,5.53
21/08/2017 18:00:00,5.58
21/08/2017 19:00:00,4.80
21/08/2017 20:00:00,4.59
21/08/2017 21:00:00,3.72
21/08/2017 22:00:00,3.95
21/08/2017 23:00:00,3.11
22/08/2017 00:00:00,3.07
22/08/2017 01:00:00,2.80
22/08/2017 02:00:00,2.75
22/08/2017 03:00:00,2.79
22/08/2017 04:00:00,2.76
22/08/2017 05:00:00,2.76
22/08/2017 06:00:00,3.06
22/08/2017 07:00:00,3.88
我想遍历特定时间点的每一行(比如说8月22日午夜),所以我尝试像这样实现它:
I want to loop through every row from a specific point in time on (let's say midnight of August 22nd), so I tried implementing it like this:
df = pandas.read_csv('file.csv')
start_date = '22/08/2017 00:00:00'
// since it's sorted, I figured I could use binary search
result = pandas.Series(df['Date']).searchsorted(start_date)
结果[0]
实际上给了我正确的数字.
result[0]
actually gives me the correct number.
我想我能做的就是增加该数字并通过 df.iloc [[x]]
访问该行,但是我觉得这样做很脏.
I guess what I could do then is just increment that number and access the row through df.iloc[[x]]
, but I feel dirty doing that.
for x in range(result[0], len(df)):
row = df.loc[[x]]
到目前为止,我发现的所有答案仅显示了如何迭代整个表.
All answers I've found so far only show how to iterate the whole table.
推荐答案
只需在调用 iterrows()
之前过滤数据框:
Just filter your dataframe before calling iterrows()
:
df['Date'] = pandas.to_datetime(df['Date'])
for idx, row in df[df['Date'] >= '2017-08-22'].iterrows():
#
# Whatever you want to do in the loop goes here
#
请注意,没有必要将过滤参数'2017-08-22'
转换为 datetime
对象,因为熊猫可以处理
Note that it isn't necessary to convert the filtering argument '2017-08-22'
to a datetime
object, because Pandas can handle partial string indexing.
这篇关于 pandas 从指定的行号遍历行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!