How do xor a dataframe slice with the next num and insert the answer in column 'dr'

huangapple go评论60阅读模式
英文:

How do xor a dataframe slice with the next num and insert the answer in column 'dr'

问题

I will only translate the provided content without addressing your request. Here is the translation:

好的,我有这个数据帧,你会注意到它的名称是solve,我正在使用4的倍数切片。

在“rst”列中,我想要对1进行异或3并获得2(1^3=2)。然后我想要做3^7=4,我想要将这些值放在“dr”中的相应位置。所以,solve.loc[0:, ('dr')] = 2,以及solve.loc[4:, ('dr')] = 4。我的当前方法很繁琐,不够自动化,这是我正在做的:

In [13150]: np.array(solve.loc[::4, ('rst')]) ^ np.array(solve.loc[4::4, ('rst')])
ValueError: operands could not be broadcast together with shapes (16,) (15,) 

这个错误可以通过以下解决:

In [13159]: wutwut = np.array(solve.loc[::4, ('rst')])[:15] ^ np.array(solve.loc[4::4, ('rst')])
Out[13159]: 
array([ 2,  4,  2, 11,  2,  8,  0,  0,  7,  0,  6,  0,  8, 14,  2],
      dtype=int8)

然后将这些值放回solve.loc['dr']是个问题,因为我必须手动设置长度,就像这样:

solve.loc[:56:4, ('dr')] = wutwut

如你所见,我必须手动设置长度,是否有更自动化的方式?

正如你所看到的,这很繁琐,不够实际,因为我正在处理不同和不断变化的长度,我需要一个更自动化的最佳实践方法。我正在寻求一些建议,提前感谢。此外,我还有更高级的用例,其中需要在列之间进行异或,如果有人有相关策略,对我以后的工作也会有帮助。

英文:

Ok I have this data frame which you notice is names solve and I'm using a slice of 4

In [13147]: solve[::4]
Out[13147]: 
    rst  dr
0     1   0
4     3   0
8     7   0
12    5   0
16   14   0
20   12   0
24    4   0
28    4   0
32    4   0
36    3   0
40    3   0
44    5   0
48    5   0
52   13   0
56    3   0
60    1   0

What I want is to in column 'rst' xor 1 by 3 and get 2 (1^3=2). then I want to do 3^7 = 4, I want to put those in the corresponding spots in dr. so solve.loc[0:, ('dr')] = 2, and solve.loc[4:, ('dr')] = 4. My current method is tedious and not automatic, here is what I'm doing:

In [13150]: np.array(solve.loc[::4, ('rst')]) ^ np.array(solve.loc[4::4, ('rst')])
ValueError: operands could not be broadcast together with shapes (16,) (15,) 

which is resolved with:

In [13159]: wutwut = np.array(solve.loc[::4, ('rst')])[:15] ^ np.array(solve.loc[4::4, ('rst')])
Out[13159]: 
array([ 2,  4,  2, 11,  2,  8,  0,  0,  7,  0,  6,  0,  8, 14,  2],
      dtype=int8)

and then putting the values back into solve.loc['dr'] is an issue because I have to bust a length in manually like:

solve.loc[:56:4, ('dr')] = wutwut

see I have to manually set the length, is there a more automatic way

As you can see this is tedious and not practical because I'm working with different and changing lengths and I need a more automatic best practice cast for this. I'm looking for some suggestions and thanks in advance. Also I have more advanced use cases where is xor between columns so if anyone has strategies for that it will help me down the road as well

答案1

得分: 2

Here is the translation of the provided code:

让我们使用 "loc" 进行索引和自动赋值:

s = solve.loc[::4, 'rst']
solve.loc
展开收缩
, 'dr'] = s[:-1].values ^ s[1:].values

     rst  dr
0     1   2
4     3   4
8     7   2
12    5  11
16   14   2
20   12   8
24    4   0
28    4   0
32    4   7
36    3   0
40    3   6
44    5   0
48    5   8
52   13  14
56    3   2
60    1   0

I hope this helps!

英文:

Lets use loc for indexing and automatic value assignment

s = solve.loc[::4, 'rst']
solve.loc
展开收缩
, 'dr'] = s[:-1].values ^ s[1:].values

    rst  dr
0     1   2
4     3   4
8     7   2
12    5  11
16   14   2
20   12   8
24    4   0
28    4   0
32    4   7
36    3   0
40    3   6
44    5   0
48    5   8
52   13  14
56    3   2
60    1   0

huangapple
  • 本文由 发表于 2023年4月11日 07:29:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75981439.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定