简体   繁体   中英

Vectorized method of converting binary to int for pandas dataframe/series

Everything I've searched for has only yielded apply as a solution, which I know is not really an optimized method. For example:

df['c1'].apply(lambda x: int(x2, 2))

I've been looking at the doc pages for pd.Series but can't find anything so far.

Is there a faster way to do this?

Edit: Turn this:

0         11111
1         00000
2         00000
3         00010
4         00011
5         00100
6         00000
7         00001
8         01001
9         00000
10        00111
11        10111
12        11001
13        01001
14        01100
15        01100
16        00000
17        00110
18        10101
19        10101
20        01011
21        01110
22        01110
23        10101
24        00001
25        01001
26        01010
27        00000
28        00000
29        00000
          ...  
139861    01000
139862    10000
139863    00100

Into this:

0         31
1         0 
2         0 
3         2 
4         3 
5         4 
6         0 
7         1 
8         9 
9         0 
10        7 
11        23
12        25
13        9 
14        12
15        12
16        0 
17        6 
18        21
19        21
20        11
21        14
22        14
23        21
24        1 
25        9 
26        10
27        0 
28        0 
29        0 
         .. 
139861    8 
139862    16
139863    4 

I don't imagine your original...

df['col1'].apply(lambda x: int(x2, 2))

...is going to be painfully slow.. however, you can avoid the lambda overhead by using args= in apply, eg:

df.col1.apply(int, args=(2,))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM