2

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 
3
  • Can you please paste a cutout of your data frame so we know what your data looks like and what you want to do? Commented Aug 18, 2017 at 19:12
  • 1
    Where do you read that apply is not optimized? The documentation is not saying that (at least not explicitly): pandas.pydata.org/pandas-docs/stable/generated/… Commented Aug 18, 2017 at 19:15
  • 1
    If you extract the underlying numpy array you can follow the methods presented in stackoverflow.com/questions/37373017/… Commented Aug 18, 2017 at 19:31

1 Answer 1

0

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,))
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, I've just been reading a lot that using .str methods, and .np methods will always be faster than using apply and lambda and wondered if there was a similar trick I could use here for binary -> int
@MattTakao I'm not aware of any that convert base-2 strings into integers though... I think the above is as good as it gets for the moment. (Although I'd be very happy to be corrected)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.