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]
Post a Comment for "Apply Function On Pairs Of Rows In Pandas Dataframe"