What it is the most efficient way to shift and add extra row of data to match delta using python pandas -


what efficient way use pandas align , shift data add row 0 in or b columns if difference higher 2 abs, in order recursively make difference of a-b < 2 ex.-

[column a]  [diff - b]   [column b]  0                0             0  4.54             4.54          0       <-- need add 0 shift 4.54             0             4.54 4.54             0             4.54 4.54            -3.04          1.5       after added   [column a]  [diff - b]   [column b]      0                0             0      0                0             0      0               -4.54          4.54    <--recursive need same     4.54             0             4.54     4.54             0             4.54     4.54            -3.04          1.5 

the whole idea push data match on lower diff. if notice need add , shift every time find diff absolute higher 2 on column or column b

  • split dataframe before , after
    df0, df1 = df.iloc[:1], df.iloc[1:]
  • create series of goes in between
    s = pd.series(0, df.columns).to_frame().t
  • concatenate 3
    pd.concat([df0, s, df1], ignore_index=true)

df0, df1 = df.iloc[:1], df.iloc[1:] s = pd.series(0, df.columns).to_frame().t pd.concat([df0, s, df1], ignore_index=true) 

enter image description here


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -