简体   繁体   English

用于熊猫数据帧/系列的将二进制转换为int的矢量化方法

[英]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. 我搜索过的一切都只能得到apply的解决方案,我知道是不是真的的优化方法。 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. 我一直在寻找pd.Series的文档页面,但到目前为止找不到任何东西。

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: ...将会非常缓慢。.但是,您可以通过在apply中使用args=来避免lambda开销,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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