Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.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字符串->;十六进制->;整数->;字符串转换_Python_String_Pandas_Hex_Decimal - Fatal编程技术网

Python字符串->;十六进制->;整数->;字符串转换

Python字符串->;十六进制->;整数->;字符串转换,python,string,pandas,hex,decimal,Python,String,Pandas,Hex,Decimal,我有来自嵌入式硬件的数据。消息以长十六进制的形式出现,但这个十六进制实际上是硬件,它将几个不同的数据组连接在4个字符的块中。让人困惑的是,数据库正在将这个长十六进制转换为十进制,在我可以访问的层之下 我的数据帧: dict = {'A': ['foo', 'bar', 'baz'], 'B': ['1346', '9953', '5246']} df = pd.DataFrame(dict) 我想将列B转换为一个称为LongHex的十六进制数,将LongHex的最后4个字符拆分为ShortHe

我有来自嵌入式硬件的数据。消息以长十六进制的形式出现,但这个十六进制实际上是硬件,它将几个不同的数据组连接在4个字符的块中。让人困惑的是,数据库正在将这个长十六进制转换为十进制,在我可以访问的层之下

我的数据帧:

dict = {'A': ['foo', 'bar', 'baz'], 'B': ['1346', '9953', '5246']}
df = pd.DataFrame(dict)
我想将列
B
转换为一个称为
LongHex
的十六进制数,将
LongHex
的最后4个字符拆分为
ShortHex
,将
ShortHex
转换回一个名为
ShortDec
的整数,最后用
=“”
,这样Excel就不会将某些十六进制值转换为科学符号

以下是我到目前为止所做的尝试:

df['LongHex'] = df['B'].apply(lambda x: hex)
df['ShortHex'] = df['LongHex'].str[-4:]
df['ShortDec'] = df['ShortHex'].apply(lambda x: int)
df['LongHex'] = df['LongHex'].apply(str)
df['ShortHex'] = df['ShortHex'].apply(str)
df['LongHex'] = df['LongHex'].apply(lambda x: '="' + x + '"')
df['ShortHex'] = df['ShortHex'].apply(lambda x: '="' + x + '"')
最终,该数据帧被输出到.csv。当我打开文件时,我得到的是:

foo, 1346, <built-in function hex>, nan, <type 'int'>
bar, 9953, <built-in function hex>, nan, <type 'int'>
baz, 5246, <built-in function hex>, nan, <type 'int'>
foo,1346,南,
巴,9953,南,
巴兹,5246,南,
更改此选项:

df['LongHex'] = df['B'].apply(lambda x: hex)
df['ShortDec'] = df['ShortHex'].apply(lambda x: int)
为此:

df['LongHex'] = df['B'].apply(lambda x: hex(x))
df['ShortDec'] = df['ShortHex'].apply(lambda x: int(x))
另外,作为旁注,我看到您稍后将它们转换为字符串,为什么不一次完成所有操作呢

df['LongHex'] = df['B'].apply(lambda x: str(hex(x))[-4:])