首页 > 解决方案 > 按间隔拆分行 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"])       

标签: pythonpandas

解决方案


推荐阅读