Skip to content Skip to sidebar Skip to footer

How To Execute A Function On A Group Of Rows In Pandas Dataframe?

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, i.e. (x,y) coo

Solution 1:

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

Post a Comment for "How To Execute A Function On A Group Of Rows In Pandas Dataframe?"