将 Pandas 数据框列从十六进制字符串转换为 int
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37955856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
convert pandas dataframe column from hex string to int
提问by kaminsknator
I have a very large dataframe that I would like to avoid iterating through every single row and want to convert the entire column from hex string to int. It doesn't process the string correctly with astype but has no problems with a single entry. Is there a way to tell astype the datatype is base 16?
我有一个非常大的数据框,我想避免遍历每一行并希望将整个列从十六进制字符串转换为整数。它不会使用 astype 正确处理字符串,但单个条目没有问题。有没有办法告诉 astype 数据类型是 base 16?
IN:
import pandas as pd
df = pd.DataFrame(['1C8','0C3'], columns=['Command0'])
df['Command0'].astype(int)
OUT:
ValueError: invalid literal for int() with base10: '1C8'
This works but want to avoid row iteration.
这有效,但要避免行迭代。
for index, row in df.iterrows():
print(row['Command0'])
I'm reading this in from a CSV pd.read_csv(open_csv, nrows=20)
so if there is a way to read it in and explicitly tell it what the format is then that would be even better!
我正在从 CSVpd.read_csv(open_csv, nrows=20)
中读取它,所以如果有一种方法可以读取它并明确地告诉它格式是什么,那就更好了!
回答by andrew
You could use apply
.
你可以使用apply
.
df.Command0.apply(lambda x: int(x, 16))
>>>
0 456
1 195
Name: Command0, dtype: int64
And you can do this with pd.read_csv
call using the converters
parameter:
你可以pd.read_csv
使用converters
参数调用来做到这一点:
df = pd.read_csv("path.txt", converters={"Command0": lambda x: int(x, 16)})
回答by jpp
You can use apply
as per @Andrew's solution, but lambda
isn't necessary and adds overhead. Instead, use apply
with a keyword argument:
您可以apply
按照@Andrew 的解决方案使用,但lambda
不是必需的并且会增加开销。相反,使用apply
关键字参数:
res = df['Command0'].apply(int, base=16)
print(res)
0 456
1 195
Name: Command0, dtype: int64
With pd.read_csv
, you can use functools.partial
:
使用pd.read_csv
,您可以使用functools.partial
:
from functools import partial
df = pd.read_csv(open_csv, nrows=20, converters={'Command0': partial(int, base=16)})
回答by mirekphd
The reverse operation (float to hex lossless conversion) would be:
反向操作(浮点到十六进制无损转换)将是:
df['Command0'].apply(float.hex)
df['Command0'].apply(float.hex)