通过函数的结果替换值

Poi*_*nec 2 python dataframe python-3.x pandas

我有以下数据框表:

df = pd.DataFrame({'A': [0, 1, 0],
                   'B': [1, 1, 1]},
                  index=['2020-01-01', '2020-02-01', '2020-03-01'])
Run Code Online (Sandbox Code Playgroud)

我试图实现存在 1 的每个值都将被越来越多的数字所取代。我正在寻找类似的东西:

df.replace(1, value=3)
Run Code Online (Sandbox Code Playgroud)

效果很好,但我需要增加数字而不是数字 3(因为我想将它用作 ID)

number += 1
Run Code Online (Sandbox Code Playgroud)

如果我将它们连接在一起,它不起作用(或者至少我无法找到正确的语法)我想获得以下结果:

df = pd.DataFrame({'A': [0, 2, 0],
                   'B': [1, 3, 4]},
                  index=['2020-01-01', '2020-02-01', '2020-03-01'])
Run Code Online (Sandbox Code Playgroud)

注意:我不能使用任何依赖于列名或行名规范的命令,因为表有 2600 列和 5000 行。

cbh*_*ang 5

df.values可以工作的副本上的元素分配。

更具体地说,range11 的数量(包括)开始分配到1值数组中元素的位置。然后将分配的数组放回原始数据帧中。

代码

(数据如给定)

1. 行优先排序(OP 想要什么)

arr = df.values
mask = (arr > 0)
arr[mask] = range(1, mask.sum() + 1)
for i, col in enumerate(df.columns):
    df[col] = arr[:, i]

# Result
print(df)
            A  B
2020-01-01  0  1
2020-02-01  2  3
2020-03-01  0  4
Run Code Online (Sandbox Code Playgroud)

2. 列优先排序(另一种可能)

arr_tr = df.values.transpose()
mask_tr = (arr_tr > 0)
arr_tr[mask_tr] = range(1, mask_tr.sum() + 1)
for i, col in enumerate(df.columns):
    df[col] = arr_tr[i, :]

# Result
print(df)
            A  B
2020-01-01  0  2
2020-02-01  1  3
2020-03-01  0  4
Run Code Online (Sandbox Code Playgroud)