parzival
parzival

Reputation: 21

How to create a column based of the bitwise XOR of comparing two columns pandas

I am trying to fill a premade column with the bitwise XOR of two other columns

for index2, row in spec.iterrows():
    registerName = spec.loc[index2, 'Register Name']
    registerAddr = spec.loc[index2, 'CPU Address']
    registerAddr = '0x' + registerAddr
    settings1Val = output.loc[output['CPU Address'] == registerAddr, 'Settings 1 Values']
    settings2Val = output.loc[output['CPU Address'] == registerAddr, 'Settings 2 Values']
    fpgaAddr = spec.loc[index2, 'Address']
    readWrite = spec.loc[index2, 'R/W']
    output.loc[output['CPU Address'] == registerAddr, 'Register Name'] = registerName
    output.loc[output['CPU Address'] == registerAddr, 'R/W'] = readWrite
    output.loc[output['CPU Address'] == registerAddr, 'FPGA Address'] = fpgaAddr
    output.loc[output['CPU Address'] == registerAddr, 'Delta (Pos. Difference)'] = settings1Val ^ settings2Val

For the final line, I have settings1Val and settings2Val, which are just ints at a specific index within my input file. The final line works properly (it returns the bitwise XOR in int form), but when I try

bin(settings1Val ^ settings2Val)

It gives me an error:

TypeError: 'Series' object cannot be interpreted as an integer

What solution is there to get that reference in binary form?

Upvotes: 0

Views: 429

Answers (1)

user17242583
user17242583

Reputation:

Use this:

(settings1Val ^ settings2Val).apply(bin)

Or, if you want to strip the leading 0b:

(settings1Val ^ settings2Val).apply(bin).str[2:]

Or, as @JonClements suggested, you also use this to do it without the leading 0b:

(settings1Val ^ settings2Val).apply('{:b}'.format)

Upvotes: 1

Related Questions