1 回答

TA贡献1872条经验 获得超3个赞
首先,您只能选择数字列并用in替换非0
值,然后为第二个添加累积总和,比较第一个值和布尔掩码转换为整数:1
DataFrame.mask
axis=1
1
DataFrame.eq
DataFrame.astype
df1, df2 = data.copy(), data.copy()
cols = df1.select_dtypes(include=np.number).columns
df1[cols] = df1[cols].mask(data[cols].ne(0), 1)
df2[cols] = df1[cols].cumsum(axis=1).eq(1).astype(int)
print(df1)
Name Date1 Date2 Date3 Date4 Date5
0 Juan 0 0 1 1 1
1 Luis 1 1 1 1 1
2 Maria 0 1 1 1 1
3 Laura 0 0 0 1 1
4 Lina 0 0 0 0 1
print(df2)
Name Date1 Date2 Date3 Date4 Date5
0 Juan 0 0 1 0 0
1 Luis 1 0 0 0 0
2 Maria 0 1 0 0 0
3 Laura 0 0 0 1 0
4 Lina 0 0 0 0 1
添加回答
举报