'How do you a apply a function to a cell and the cell below?
I am trying to apply a function of two arguments to a cell and the cell below.
Eg. my function is
def foo(x,y):
return f'{x} -> {y}'
# Input
s = pd.Series([1,2,3])
# Output
pd.Series(['1 -> 2', '2 -> 3', '3 -> nan'])
I'm not sure what command I could use to do this in pandas.
Notes:
If I were subtracting the values I could do
s.diff(1)If this were a vectorizable operation I could do
foo(s, s.shift(-1))I am looking for a functional style ideally (ie. avoiding an explicit loop) - just a style preference.
The function
fooabove is illustrative, I'm looking for something that works for any arbitrary function of two arguments (in my usecase I'm trying to compute text similarity using a functionfoo(text1:str, text2:str)->float)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
