将函数应用于Pandas数据框中的成对的行 [英] Apply function on pairs of rows in Pandas dataframe
问题描述
我是pandas数据框的新手,我想应用一个函数在同一列中包含几行.就像当您应用函数diff()时一样,但是我想计算文本之间的距离.所以我定义了一个测量距离的函数,我尝试使用apply,但是我不知道如何挑选几行.在下面,我显示了一个我尝试做过的例子以及我的期望:
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 distance between text. so i defined a function which measure the distance, and i tried to use apply but i don't know how can i pick couple of rows. Below i show an example that i'have tried to do and what i expected:
def my_measure_function(x,y):
return some_distance_calculus(x,y)
>>> from pandas import DataFrame
>>> df = DataFrame({"text": ['hello','hella','hel'], "B": [3,4,4]})
>>> df['dist'] = df.apply(lambda x, y: my_measure_function(x, y), axis=0)
但是它不起作用. 我想获得的是:
but it doesn't work. What i want to obtain is:
>>> df
text B dist
0 hello 3 0
1 hella 4 1
2 hel 4 2
在此先感谢您能为我提供的任何帮助.
Thanks in advance for any help that you can provide me.
推荐答案
您可能希望避免性能创建新列可能会受到影响.相反,您可以将map
与 pd.Series.shift
结合使用:
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()))
或通过列表理解:
zipper = zip(df['text'], df['text'].shift())
df['dist'] = [my_measure_function(val1, val2) for val1, val2 in zipper]
这篇关于将函数应用于Pandas数据框中的成对的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!