Tuesday, 15 July 2014

python - Performing bitwise operations on a dataframe -



python - Performing bitwise operations on a dataframe -

i have pandas dataframe this

in [7]: xyz out[7]: 0 8495872 1 8496128 2 8561408 3 8561664 4 8626944 5 8627200 6 8692736 7 8692992 8 8693248 9 8693504 10 8758016 11 8758272 12 8758528 13 8758784 14 8759040 15 8823296 16 8823552 17 8823808 18 8824064 19 8824320 20 8824576 21 8824832 22 8888832 23 8890112 name: xyz, dtype: int64

i want convert these values 32bit binary. have tried xyz.apply(bin) function gives this

0 0b100000011010001100000000 1 0b100000011010010000000000 2 0b100000101010001100000000 3 0b100000101010010000000000 ...

there unwanted '0b' in front end , output 24 bit. how these values converted 32 bit without '0b' part in front?

after conversion, going bitwise , / or operations values , generate new dataframe.

if going manipulate bits, don't need convert them first. utilize bitwise_and , bitwise_or numpy functions on dataframe's column:

in [14]: import numpy np in [15]: df['col'] out[15]: 0 5302274 1 4767983 2 6158485 3 9807950 4 4360582 5 2156122 ... in [16]: np.bitwise_and(df['col'], 0b101010) out[16]: 0 2 1 42 2 0 3 10 4 2 5 10

pandas depends on numpy, should have installed.

python pandas binary dataframes

No comments:

Post a Comment