Working on data frame, which contains segments( with two endpoints ), I have to find mid points for each segment, and finally insert a row in the df with the mid point coordinates between the two end point rows.
Below is the df:
id x y
0 1 0.8000 1.90
1 1 0.8833 2.00
2 2 1.0000 2.14
3 2 1.3000 2.50
Points with the same id are the end points of the same segment.
Have created the following simple func (basically calculatingd Mean) :
def find_mpt(x1, y1, x2, y2):
x, y = ( x1 + x2) / 2 , (y1 + y2) / 2
return x, y
Want to apply the func to entire df, and insert the resultant rows specifically between the end point rows, as following:
id x y
0 1 0.8000 1.90
1 1 0.8416 1.95 #new row
2 1 0.8833 2.00
3 2 1.0000 2.14
4 2 1.1500 2.32 #new row
5 2 1.3000 2.50
Maybe I can use df.groupby(['id']) and then apply the function, but still have no idea how to insert rows at those specific locations.
df.apply(lambda x: find_mpt(x[0], x[2], x[1], x[3])), but I don't know how you would go about inserting them into the dataframe without creating a new dataframe.pd.concat((df, df.groupby('id', as_index=False).mean())).sort_values(['id', 'x'])to get this output but I am making a few assumptions: 1) you want the mean, 2) you want to sort the id, 3) you have no negative values (so when sorting by x the average will be in the middle)