iterrows pandas get next rows value

前端 未结 5 1060
南方客
南方客 2020-12-23 09:50

I have a df in pandas

import pandas as pd
df = pd.DataFrame([\'AA\', \'BB\', \'CC\'], columns = [\'value\'])

I want to iterate over rows in

5条回答
  •  一生所求
    2020-12-23 10:08

    a combination of answers gave me a very fast running time. using the shift method to create new column of next row values, then using the row_iterator function as @alisdt did, but here i changed it from iterrows to itertuples which is 100 times faster.

    my script is for iterating dataframe of duplications in different length and add one second for each duplication so they all be unique.

    # create new column with shifted values from the departure time column
    df['next_column_value'] = df['column_value'].shift(1)
    # create row iterator that can 'save' the next row without running for loop
    row_iterator = df.itertuples()
    # jump to the next row using the row iterator
    last = next(row_iterator)
    # because pandas does not support items alteration i need to save it as an object
    t = last[your_column_num]
    # run and update the time duplications with one more second each
    for row in row_iterator:
        if row.column_value == row.next_column_value:
             t = t + add_sec
             df_result.at[row.Index, 'column_name'] = t
        else:
             # here i resetting the 'last' and 't' values
             last = row
             t = last[your_column_num]
    

    Hope it will help.

提交回复
热议问题