Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python和Pandas在十六进制到数字转换方面遇到了麻烦_Python_Pandas_Hex_Python 2.x - Fatal编程技术网

Python和Pandas在十六进制到数字转换方面遇到了麻烦

Python和Pandas在十六进制到数字转换方面遇到了麻烦,python,pandas,hex,python-2.x,Python,Pandas,Hex,Python 2.x,这可能真的很愚蠢,因为我已经离开python 10年了 我在一个名为rebal的数据框架上应用了以下函数,该数据框架有几列包含十六进制字符串数据 def hex2page(s, pagesize=1): if(s[:2]=='0x'): i=long(s,16)*pagesize #print(s, i) return i return None 这里是一个成功的运行,你可以看到一个新的列“分配”,在最右边的直接转换为十进制;p

这可能真的很愚蠢,因为我已经离开python 10年了

我在一个名为rebal的数据框架上应用了以下函数,该数据框架有几列包含十六进制字符串数据

def hex2page(s, pagesize=1):
    if(s[:2]=='0x'): 
        i=long(s,16)*pagesize
        #print(s, i)
        return i
    return None

这里是一个成功的运行,你可以看到一个新的列“分配”,在最右边的直接转换为十进制;p>

rebal['Allocated']=rebal['Alc_PG'].apply(hex2page)
rebal.head()

            Time Stamp       LOG  POOL  Cap T_DEVNum E_DEVNum    HDEV   Alc_PG   T_MVPG   E_MVPG     0DPG       Hist  Allocated
0  2013/08/20 08:51:50  00642fff     0  ---      ---      ---  0x7c8a      ---      ---  0x0000d  0x00000    DPV END        NaN
1  2013/08/20 08:51:50  00642fff     0  ---      ---      ---  0x7c8b  0x002e0  0x0008d      ---      ---  DPV START        736
2  2013/08/20 08:51:47  00642ffe     0  ---      ---      ---  0x7c89      ---      ---  0x00018  0x00000    DPV END        NaN
3  2013/08/20 08:51:47  00642ffe     0  ---      ---      ---  0x7c8a  0x002b6  0x0000d      ---      ---  DPV START        694
4  2013/08/20 08:51:40  00642ffd     0  ---      ---      ---  0x7c89  0x002a0  0x00018      ---      ---  DPV START        672
所以它在列Alc_PG上工作,但是如果我尝试另一个具有相同字符串格式的列,它将无法为每个十六进制字符串返回零

rebal['T_MVPG'].apply(hex2page)

0    NaN
1      0
2    NaN
3      0
4      0
5    NaN
但如果我在上述函数中取消对print的注释,它将打印正确的转换

('0x0008d', 141L)
('0x0000d', 13L)
('0x00018', 24L)
('0x00070', 112L)
('0x00073', 115L)
('0x00011', 17L)
('0x00062', 98L)
所以我很困惑。我甚至在那里有指纹来检查类型,s肯定是作为str输入函数的

有人有什么想法吗

rebal[0:5].to_dict()

{'0DPG': {0: '0x00000', 1: '---', 2: '0x00000', 3: '---', 4: '---'},
 'Alc_PG': {0: '---', 1: '0x002e0', 2: '---', 3: '0x002b6', 4: '0x002a0'},
 'Allocated': {0: nan, 1: 736.0, 2: nan, 3: 694.0, 4: 672.0},
 'Cap': {0: '---', 1: '---', 2: '---', 3: '---', 4: '---'},
 'E_DEVNum': {0: '---', 1: '---', 2: '---', 3: '---', 4: '---'},
 'E_MVPG': {0: '0x0000d', 1: '---', 2: '0x00018', 3: '---', 4: '---'},
 'HDEV': {0: '0x7c8a', 1: '0x7c8b', 2: '0x7c89', 3: '0x7c8a', 4: '0x7c89'},
 'Hist': {0: 'DPV END',
  1: 'DPV START',
  2: 'DPV END',
  3: 'DPV START',
  4: 'DPV START'},
 'LOG': {0: '00642fff',
  1: '00642fff',
  2: '00642ffe',
  3: '00642ffe',
  4: '00642ffd'},
 'POOL': {0: 0, 1: 0, 2: 0, 3: 0, 4: 0},
 'T_DEVNum': {0: '---', 1: '---', 2: '---', 3: '---', 4: '---'},
 'T_MVPG': {0: '---', 1: '0x0008d', 2: '---', 3: '0x0000d', 4: '0x00018'},
 'Target': {0: nan, 1: 141.0, 2: nan, 3: 13.0, 4: 24.0},
 'Time Stamp': {0: '2013/08/20 08:51:50',
  1: '2013/08/20 08:51:50',
  2: '2013/08/20 08:51:47',
  3: '2013/08/20 08:51:47',
  4: '2013/08/20 08:51:40'}}

我想你必须在调用hex2page的地方发布代码。在这里似乎工作得很好,你使用的是哪个版本的pandas?它在ipython笔记本中运行,所以代码如上所述。pandas的版本是11.0,是最新anaconda的一部分。@user2713369是的,但是当我通过复制和粘贴数据帧来完成这项工作时,你给它的代码是有效的!所以有点可疑。输出列可能有帮助,或者包括rebal.to_dict的输出,可能两者都有