按间隔熊猫划分行

时间:2020-03-16 04:18:50

标签: python pandas

我的数据

Start  End    Length            Number of Sections
0.01   0.03    0.02(End- Start)    0.02/0.01=2.0
0.00   0.05    0.05                0.05/0.01=5.0   

我希望输出为

Start End      Length           Number of Sections
0.01  0.02      0.01              1.0
0.02  0.03       0.01              1.0
0.00  0.01       0.01               1.0
0.01  0.02       0.01                1.0
0.02  0.03       0.01                 1.0
0.03  0.04       0.01                  1.0
0.04  0.05       0.01                   1.0 

尝试了很多次。请给一些建议

bounds=np.unique(np.hstack((df["StartMilepost"],df["EndMilepost"])))

df2=pd.DataFrame({"StartMilepost":bounds[:-1],"EndMilepost":bounds[1:]})

isin=df.apply(lambda x :
              df2['StartMilepost'].between(int(x[0],x[1]-1)) 

              | df2['EndMilepost'].between(int(x[0]+1,x[1]),axis=1)).T

data=np.where(isin,df.NumberofSections,0)

df3=pd.DataFrame(data,
pd.MultiIndex.from_arrays(df2.values.T),df["min"])       

0 个答案:

没有答案