我有一张桌子:
Object | Col1 | Col2 | Col3 | Col4 |
---|---|---|---|---|
reference | 10 | 14 | 7 | 29 |
Obj1 | 0 | 9 | 1 | 30 |
Obj2 | 1 | 16 | 0 | 17 |
Obj3 | 9 | 21 | 3 | 0 |
Obj4 | 11 | 0 | 4 | 22 |
我想按条件转换它:如果任何单元格(第一行的单元格除外)=0,则必须将其替换为本列第一行的递增(X+1)值.
得到的表格是:
Object | Col1 | Col2 | Col3 | Col4 |
---|---|---|---|---|
reference | 10 | 14 | 7 | 29 |
Obj1 | 11 | 9 | 1 | 30 |
Obj2 | 1 | 16 | 8 | 17 |
Obj3 | 9 | 21 | 3 | 30 |
Obj4 | 11 | 15 | 4 | 22 |
我试过这个变种:
df = np.where(df[df == 0] == 0, df.iloc[0] + 1, df)
但结果是ndarray
,而不是DataFrame,性能不够好.
有没有办法只用Pandas 的工具就能做到这一点呢?