将 pandas 数据帧中的十六进制字符串拆分为 4 个字节
我有一个像这样的数据框:
idx cola colb hexstring
0 2 a 2000001443b660280c25800380241c0000102000120000000000000000000003010
1 3 b 80b7d0082b7d0082b7d00821d640000102000
2 5 a ffffffff34140038030000014
...
我想用 4 个字节分割十六进制字符串列,并用 0 填充其余部分,这样,
idx cola colb hexstring
0 2 a 00003010
0 2 a 00000000
0 2 a 00000000
0 2 a 02000120
0 2 a 41c00001
0 2 a 58003802
0 2 a 660280c2
0 2 a 0001443b
0 2 a 00000200
1 3 b 00102000
1 3 b 821d6400
1 3 b 0082b7d0
1 3 b 082b7d00
...
有没有办法做到这一点?
I have a dataframe like this:
idx cola colb hexstring
0 2 a 2000001443b660280c25800380241c0000102000120000000000000000000003010
1 3 b 80b7d0082b7d0082b7d00821d640000102000
2 5 a ffffffff34140038030000014
...
And I want to split the hexstring column with 4 byte, and fill with the rest with 0, such that,
idx cola colb hexstring
0 2 a 00003010
0 2 a 00000000
0 2 a 00000000
0 2 a 02000120
0 2 a 41c00001
0 2 a 58003802
0 2 a 660280c2
0 2 a 0001443b
0 2 a 00000200
1 3 b 00102000
1 3 b 821d6400
1 3 b 0082b7d0
1 3 b 082b7d00
...
Is there a way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不是特别了解字节等,但在这里我的解决方案将带您到达目的地。据我了解,您想将字符串从后面分成 8 个字符的片段。但如果初始字符不够 8 个,则需要在前面添加零。
我的解决方案是先在前面添加零,使字符串变成八的倍数。
zfill 在字符串前面添加指定数量的零。对于金额,我通过将字符串的长度除以 8 并获取最接近的较大整数来计算。现在你已经有了 8 次的所有字符串。
然后我将字符串分成 8 个一组并将其保存在一个列表中。之后,您可以分解列表,以便可以将每个组放在单独的行中。
当然,这里的结果与您想要的相反,因为列表从一开始就爆炸了。如果您更喜欢精确的目标,则可以在分解列表之前反转列表。
I am not particularly aware of bytes and such, but here my solution will get you to your destination. From what I understand is you want to split your string into pieces of 8 characters from the back. But if the initial chracters will not have 8 enough, you want to add zeros in front.
My solution here make your strings into times of eight first by adding zeros in front.
zfill adds zeros in front of the string for the specified amount. For the amount, I calculate by dividing the length of the string wih 8 and getting the nearest higher integer. Now you have all strings in times of 8.
Then I split the string into groups of eights and keep it in a list. Afterwards you can just explode the lists so that you can get each groups in seperate rows.
Of course, here the results are the reverse of what you want because the lists explode from the beginning. If you prefer your exact target, you can reverse the lists before exploding them.