Converting a list of strings to list of int in pandas

2

after reading CSV file, I have a column that has a string of comma-separated hexadecimal values. For e.g. "0x00000034,0x0000008a,0x00000145". I want to convert this column to a list of integers. I am able to split and convert to a list of integers using below code:

for index, row in df.iterrows():
    rnti_list = [int(idx, 16) for idx in row['rnti'].split(",")]

I can then add this as a new column.

Wanted to know the most elegant code which would accomplish the same with minimum number of lines.

Thanks.

python
pandas
asked on Stack Overflow Oct 17, 2018 by Nikhil Utane

2 Answers

1

Use nested list comprehension:

df = pd.DataFrame({'rnti':['0x00000034,0x0000008a,0x00000145','0x00000034,0x0000008a']})
print (df)
                               rnti
0  0x00000034,0x0000008a,0x00000145
1             0x00000034,0x0000008a

rnti_list = [[int(idx, 16) for idx in x.split(",")] for x in df['rnti']]
print (rnti_list)
[[52, 138, 325], [52, 138]]

df['new'] = [[int(idx, 16) for idx in x.split(",")] for x in df['rnti']]
print (df)
                               rnti             new
0  0x00000034,0x0000008a,0x00000145  [52, 138, 325]
1             0x00000034,0x0000008a       [52, 138]
answered on Stack Overflow Oct 17, 2018 by jezrael • edited Oct 17, 2018 by jezrael
1

Using apply

Ex:

import pandas as pd

df = pd.DataFrame({"rnti": ["0x00000034,0x0000008a,0x00000145"]})    
print( df["rnti"].str.split(",").apply(lambda x: [int(i, 16) for i in x]) )

Output:

0    [52, 138, 325]
Name: rnti, dtype: object
answered on Stack Overflow Oct 17, 2018 by Rakesh

User contributions licensed under CC BY-SA 3.0