首页 > 解决方案 > 数据操作 Python - 迭代行

问题描述

这是我的数据:

S.No StartMilepost  End Milepost  Segment Length  
1     0.01           0.03          0.02 

我想要这样的输出:

S.No StartMilepost  End Milepost  Segment Length  
1     0.01           0.02          0.01
2     0.02           0.03          0.01   

我试过这段代码:

split_rows = (df2['SegmentLength'] / 0.01)  

while split_rows.any():
    df2.loc[split_rows,'StartMilepost']=df2.loc[split_rows, 'StartMilepost']

    #update the end Milepost of old rows
    df2.loc[split_rows, 'EndMilepost'] = df2.loc[split_rows, 'StartMilepost'] 
          +0.01

     #update the duration of all rows
    df2['SegmentLength'] = df2['EndMilepost'] - df2['StartMilepost']

    #when job is done:

df2.sort_index().reset_index(drop=True)
df2 = df2.append(new_rows)

标签: pythonpandas

解决方案


推荐阅读