当您需要为每个数据框选择不同的列时,如何更改数据框值

2024-09-24 06:30:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据帧,格式如下:

W1 W2 W3 W4 W5 W6 W7 W8 0 0 1 0 1 1 1 1 0 0 1 0 0 1 1 1 0 1 0 0 1 1 0 0 1 0 0 0 1 1 0 1

有一个参数DIFF=3。 我在每一行中查找从W1到W4的列,并搜索最后的1。它将位于第W3、W3、W2、W1列。随后,我将整行1右侧的下3个(DIFF)元素更改为0。 例如,我用x标记了这些元素:

W1 W2 W3 W4 W5 W6 W7 W8 0 0 1 x x x 1 1 0 0 1 x x x 1 1 0 1 x x x 1 0 0 1 x x x 1 1 0 1

最终结果:

W1 W2 W3 W4 W5 W6 W7 W8 0 0 1 0 0 0 1 1 0 0 1 0 0 0 1 1 0 1 0 0 0 1 0 0 1 0 0 0 1 1 0 1

现在,我有一个非常复杂的解决方案,它使用iterrows(),但我正在寻找一个泛弹性的解决方案。你知道吗


Tags: 数据标记元素参数格式diff解决方案w1
2条回答

用途:

df = df.mask(df.cumsum(axis=1).ge(1).cumsum(axis=1).isin([2,3,4]), 0)
print (df)
   W1  W2  W3  W4  W5  W6  W7  W8
0   0   0   1   0   0   0   1   1
1   0   0   1   0   0   0   1   1
2   0   1   0   0   0   1   0   0
3   1   0   0   0   1   1   0   1

解释:

每行使用^{}

print (df.cumsum(axis=1))
   W1  W2  W3  W4  W5  W6  W7  W8
0   0   0   1   1   2   3   4   5
1   0   0   1   1   1   2   3   4
2   0   1   1   1   2   3   3   3
3   1   1   1   1   2   3   3   4

通过>=1^{}通信:

print (df.cumsum(axis=1).ge(1))
      W1     W2    W3    W4    W5    W6    W7    W8
0  False  False  True  True  True  True  True  True
1  False  False  True  True  True  True  True  True
2  False   True  True  True  True  True  True  True
3   True   True  True  True  True  True  True  True

再次cumsum通过boolen mask:

print (df.cumsum(axis=1).ge(1).cumsum(axis=1))
   W1  W2  W3  W4  W5  W6  W7  W8
0   0   0   1   2   3   4   5   6
1   0   0   1   2   3   4   5   6
2   0   1   2   3   4   5   6   7
3   1   2   3   4   5   6   7   8

通过2,3,4比较下3个值,并首先忽略:

print (df.cumsum(axis=1).ge(1).cumsum(axis=1).isin([2,3,4]))
      W1     W2     W3    W4     W5     W6     W7     W8
0  False  False  False  True   True   True  False  False
1  False  False  False  True   True   True  False  False
2  False  False   True  True   True  False  False  False
3  False   True   True  True  False  False  False  False

如果要定义nDIFF值,请使用更动态的解决方案:

df = pd.DataFrame({'W1': [0, 0, 0, 0], 'W2': [0, 0, 1, 0], 
                   'W3': [1, 1, 0, 0], 'W4': [0, 0, 0, 0], 
                   'W5': [1, 0, 1, 0], 'W6': [1, 1, 1, 0], 
                   'W7': [1, 1, 0, 0], 'W8': [1, 1, 0, 1]})

print (df)
   W1  W2  W3  W4  W5  W6  W7  W8
0   0   0   1   0   1   1   1   1
1   0   0   1   0   0   1   1   1
2   0   1   0   0   1   1   0   0
3   0   0   0   0   0   0   0   1

DIFF = 4
n = 3

#select columns for check by positions
subset = df.iloc[:, :n]
#replace 0 to NaNs replace back filling, change order of columns with cumsum
last_1 = subset.mask(subset == 0).bfill(axis=1).iloc[:, ::-1].cumsum(axis=1)
print (last_1)
    W3   W2   W1
0  1.0  2.0  3.0
1  1.0  2.0  3.0
2  NaN  1.0  2.0
3  NaN  NaN  NaN

#add missing columns and create ones rows by forward filling
df1 = last_1.reindex(index=df.index, columns=df.columns).ffill(axis=1)
print (df1)
    W1   W2   W3   W4   W5   W6   W7   W8
0  3.0  2.0  1.0  1.0  1.0  1.0  1.0  1.0
1  3.0  2.0  1.0  1.0  1.0  1.0  1.0  1.0
2  2.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0
3  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN

#compare by 1 and get cumsum 
print (df1.eq(1).cumsum(axis=1))
   W1  W2  W3  W4  W5  W6  W7  W8
0   0   0   1   2   3   4   5   6
1   0   0   1   2   3   4   5   6
2   0   1   2   3   4   5   6   7
3   0   0   0   0   0   0   0   0

#last check range of values
df = df.mask(df1.eq(1).cumsum(axis=1).isin(range(2, DIFF + 2)), 0)
print (df)
   W1  W2  W3  W4  W5  W6  W7  W8
0   0   0   1   0   0   0   0   1
1   0   0   1   0   0   0   0   1
2   0   1   0   0   0   0   0   0
3   0   0   0   0   0   0   0   1

以下是函数方法和泛弹性方法的混合:

df = pd.DataFrame({'w1': [0, 1, 1, 0],
                   'w2': [1, 1, 0, 1],
                   'w3': [1, 0, 0, 0],
                   'w4': [0, 1, 1, 0],
                   'w5': [1, 1, 0, 1],
                   'w6': [0, 0, 1, 1],
                   'w7': [0, 1, 1, 0],
                   'w8': [1, 1, 1, 1]})



def errase_diff(row, n = 4, Diff = 3):
    """
    returns array with erassed diff values after last positive value
    in first n column
    """
    row_length = len(row)
    last_positive_id = [i for i, v in enumerate(row[:4]) if v == 1][-1]
    row[last_positive_id + 1: last_positive_id + 1 + Diff] = [0 for _ in range(Diff)]
    return row[:row_length]


df.apply(lambda x: errase_diff(x), 1)

   w1  w2  w3  w4  w5  w6  w7  w8
0   0   1   1   0   0   0   0   1
1   1   1   0   1   0   0   0   1
2   1   0   0   1   0   0   0   1
3   0   1   0   0   0   1   0   1

请注意,此解决方案将删除原始数据框中的数据

相关问题 更多 >