I am trying to implement an algorithm . Let's say the algorithm is executed as the function "xyz"
The function is specifically designed to operate on trajectory data, ie (x,y) coordinates.
The function takes two arguments:
the first argument is a list of tuples of (x,y) points,
and the second is a constant value.
It can be illustrated as follows:
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
Output:
[(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)
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.
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..
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.
I think you need groupby
with apply
:
print (df.groupby('id')['x,y'].apply(lambda x: xyz(x, 5.0)))
Or:
print (df.groupby('id')['x,y'].apply(xyz, 5.0))
Sample with rdp
function - is necessary add tolist
, else get 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.