鉴于下面的数据框,
colNames = ["Time","Col2","Col3","Col4","Col5","Col6","Col7","Col8","Col9","Col10","Col11","Col12","Col13"]
df = pd.DataFrame(colVals, columns=colNames)
df = df.set_index('Time')
df = df.apply(pd.to_numeric, errors='coerce')
需要添加新Col14的,这样它就从 0 开始,并且每当Col11当前行的值小于前一行的值时递增Col11一 - 如果前一行的Col11值是NaN,它不应该递增Col14。
例如
+-------+-------+-------------------------------------------------------------------------------------------------+
| Col11 | Col14 | |
+-------+-------+-------------------------------------------------------------------------------------------------+
| 900 | 0 | start with 0 |
| NaN | 0 | |
| 900 | 0 | |
| 903 | 0 | |
| 904 | 0 | |
| 904 | 0 | |
| 8 | 1 | increment Col14 by 1 when current row's Col11 value is less than the previous row's Col11 value |
+-------+-------+-------------------------------------------------------------------------------------------------+
有没有办法参考上一行?
蛊毒传说
相关分类