如何基于比较两列熊猫的按位异或创建列
Posted
技术标签:
【中文标题】如何基于比较两列熊猫的按位异或创建列【英文标题】:How to create a column based of the bitwise XOR of comparing two columns pandas 【发布时间】:2022-01-12 19:08:00 【问题描述】:我正在尝试用其他两列的按位异或来填充预制列
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
对于最后一行,我有 settings1Val 和 settings2Val,它们只是我输入文件中特定索引处的整数。最后一行正常工作(它以 int 形式返回按位 XOR),但是当我尝试时
bin(settings1Val ^ settings2Val)
它给了我一个错误:
TypeError: 'Series' object cannot be interpreted as an integer
有什么解决方案可以以二进制形式获取该引用?
【问题讨论】:
你试过(settings1Val ^ settings2Val).apply(':b'.format)
吗? (或者只是.apply(bin)
,如果你想要 0b 前缀......?)
【参考方案1】:
使用这个:
(settings1Val ^ settings2Val).apply(bin)
或者,如果你想去掉前面的0b
:
(settings1Val ^ settings2Val).apply(bin).str[2:]
或者,作为@JonClements suggested,您也可以使用它来执行此操作,而无需前导0b
:
(settings1Val ^ settings2Val).apply(':b'.format)
【讨论】:
或者您可以使用.apply(':b'.format)
来避免生成前缀然后将其删除以...
非常感谢!对熊猫还是有点陌生哈哈
不错!成功了吗?以上是关于如何基于比较两列熊猫的按位异或创建列的主要内容,如果未能解决你的问题,请参考以下文章