Skip to content Skip to sidebar Skip to footer

Apply Function On Pairs Of Rows In Pandas Dataframe

I'm a newbie to pandas dataframe, and I wanted to apply a function taking couple of rows in the same column. Like when you apply the function diff(), but i want to calculate the di

Solution 1:

You may wish to avoid pd.DataFrame.apply, as performance may suffer. Instead, you can use map with pd.Series.shift:

df['dist'] = list(map(my_measure_function, df['text'], df['text'].shift()))

Or via a list comprehension:

zipper = zip(df['text'], df['text'].shift())
df['dist'] = [my_measure_function(val1, val2) for val1, val2 in zipper]

Solution 2:

For diff, which is s-s.shift(), so in your function you can do

df['shifttext']=df.text.shift()
df.apply(lambda x : my_measure_function(x['text'],x['shifttext']))

Post a Comment for "Apply Function On Pairs Of Rows In Pandas Dataframe"