数据操作Python迭代行

2024-09-30 00:41:01 发布

您现在位置: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)

Tags: ofthenosegmentupdatelengthlocrows

热门问题