python - Applying complex functions on pandas data frame -


i have df of line segments, each line segment identified unique id id , x , y coordinates.

df:

   id       x       y 0   1     0.1     0.2 1   1     0.6     1.2 2   1     2.2     1.6 3   1     2.3     1.9 4   2     0.4     0.9 5   2     0.8     1.5 6   2     1.5     1.7 7   2     1.7     2.3 8   2     1.8     3.0 

i want increase sampling points of each segment in df 8 points each, , store new points in new df.

in above df, points belonging same id 1 line segment, in df above there 2 line segments id 1 , 2.

below code shows increasing sampling points individual one line segment, line segment id = 1 above df

df2 = df[df["id"]== 1]  new_x = np.linspace(df2["x"].max(), df2["x"].min(), 8) new_y = sp.interpolate.interp1d(df2["x"], df2["y"], kind='cubic')(new_x) 

but want perform operation on whole df above, , increase sampling points each line segment. can groupby groupby(["id"]) , new_x , new_y each of line segments , store in new_df, columns id, new_x, new_y.

expected df should have line segment id , new_x , new_y columns below

  id     new_x        new_y 0  1       2.3          1.9 1  1      1.98         1.17 2  1      1.67         0.94 3  1      1.35         1.03 4  1      1.04         1.19  5  1      0.72         1.25 6  1      0.41         0.99 7  1      0.10         0.20 8  2      val          val 9  2      val          val #followed 6 more rows id 2 new_x , new_y 

please let me know if unclear.

i think @cᴏʟᴅsᴘᴇᴇᴅ can provide more efficient way , can try .

list=[] name,df2 in df.groupby("id"):     new_x = np.linspace(df2["x"].max(), df2["x"].min(), 8)     new_y = interpolate.interp1d(df2["x"], df2["y"], kind='cubic')(new_x)     new_df = pd.dataframe({'id':name,'new_x': new_x, 'new_y': new_y})     list.append(new_df)   pd.concat(list,axis=0).reset_index(drop=true)   out[55]:      id     new_x     new_y 0    1  2.300000  1.900000 1    1  1.985714  1.170057 2    1  1.671429  0.948328 3    1  1.357143  1.027302 4    1  1.042857  1.199468 5    1  0.728571  1.257314 6    1  0.414286  0.993329 7    1  0.100000  0.200000 8    2  1.800000  3.000000 9    2  1.600000  1.905774 10   2  1.400000  1.582319 11   2  1.200000  1.498203 12   2  1.000000  1.512189 13   2  0.800000  1.500000 14   2  0.600000  1.337362 15   2  0.400000  0.900000 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -