如何在 pandas 数据框中的一组行上执行功能? [英] How to execute a function on a group of rows in pandas dataframe?
问题描述
我正在尝试实现算法。假设算法是通过函数 xyz执行的。
I am trying to implement an algorithm. Let's say the algorithm is executed as the function "xyz"
该函数专门用于对轨迹数据(即(x,y)坐标)进行操作。
The function is specifically designed to operate on trajectory data, i.e. (x,y) coordinates.
该函数带有两个参数:
第一个参数是元组的列表 (x,y)分,
the first argument is a list of tuples of (x,y) points,
,第二个是常数。
可以说明如下:
line = [(0,0),(1,0),(2,0),(2,1),(2,2),(1,2),(0,2),(0,1),(0,0)]
xyz(line, 5.0) #calling the function
输出:
[(0, 0), (2, 0), (2, 2), (0, 2), (0, 0)]
只有一行时,可以轻松实现。但是我有一个庞大的数据框,如下所示:
This can be easily implemented when there is only one line. But I have a huge data frame as follows:
id x y x,y
0 1 0 0 (0,0)
1 1 1 0 (1,0)
2 1 2 0 (2,0)
3 1 2 1 (2,1)
4 1 2 2 (2,2)
5 1 1 2 (1,2)
6 2 1 3 (1,3)
7 2 1 4 (1,4)
8 2 2 3 (2,3)
9 2 1 2 (1,2)
10 3 2 5 (2,5)
11 3 3 3 (3,3)
12 3 1 9 (1,9)
13 3 4 6 (4,6)
在在上述数据框中,具有 id 的行构成了一条单独的轨迹/线的点。我想为每行实现上述功能。
In the above data frame, rows with same "id" forms the points of one separate trajectory/ line. I want to implement the above mentioned function for each of these lines.
我们可以从df中观察到ID为1,2,3的3条不同的轨迹。轨迹1在行(0-5)中具有x,y值,轨迹2在行(6-9)中具有其点,依此类推。.
We can observe from the df there are 3 different trajectories with ids 1,2,3. Trajectory 1 has its x, y value in row (0-5), trajectory 2 has its points in rows (6-9) and so on..
如何在每行中实现函数 xyz,并且由于该函数的输出再次是x,y坐标的元组列表,如何存储此列表?注意:输出列表可以包含任意数量的元组。
How to implement function "xyz" for each of these lines, and since output of this function is again a list of tuples of x,y coordinates, how to store this list? Note: The output list can contain any random number of tuples.
推荐答案
我认为您需要 groupby
与 应用
:
I think you need groupby
with apply
:
print (df.groupby('id')['x,y'].apply(lambda x: xyz(x, 5.0)))
或:
print (df.groupby('id')['x,y'].apply(xyz, 5.0))
带有 rdp
函数-必须添加到列表
,否则获取 KeyError:-1
:
print (df.groupby('id')['x,y'].apply(lambda x: rdp(x.tolist(), 5.0)))
#alternative with list
#print (df.groupby('id')['x,y'].apply(lambda x: rdp(list(x), 5.0))
id
1 [(0, 0), (1, 2)]
2 [(1, 3), (1, 2)]
3 [(2, 5), (4, 6)]
Name: x,y, dtype: object
这篇关于如何在 pandas 数据框中的一组行上执行功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!