我有一个pandas.DataFrame
看起来是这样的:
Time(minutes) column2 column1
420 1 5
420 2 10
420 3 8
421 1 4
421 2 9
421 3 7
我知道如何使用iterrows()逐行迭代,但是有没有一种有效的方法可以按列(time)中的时间单位进行迭代,以便在每次迭代中处理给定时间的数据?比如:
time = 420
while(time <= max_time):
temp <- fetch the sub-dataframe for given time
process(temp)
update original df with temp #guaranteed it won't affect any other rows other than the current set of rows
time += 1
可以使用^{} 按时间而不是按行进行迭代,如:
代码:
测试代码:
结果:
有两条路要走。第一种基本上保持迭代格式的方法是手动对数据帧进行子集划分:
另一种可能更有效的方法是使用上面由Stephen Rauch建议的
groupby
相关问题 更多 >
编程相关推荐