0

I created a dataframe where the last column shows the binary conversion of the previous column. Now I want flip the last one/two binary bits from every binary numbers of that column(R). I want flip that specific binary bit in a way that it does not affect the other bits.

How can I do that? Following is my sample code and O/P. For example, for the 1st row, I want the binary number as 001101 (from 001100) after fliping the last bit.

df = pd.DataFrame(np.random.randint(1,3,size=(10, 8)), columns=list('ABCDEFGH'))
df['Q']=df.sum(axis=1)
df['R']=df.Q.apply(lambda x: format(int(x), '06b'))

O/P:

   A  B  C  D  E  F  G  H   Q       R
0  2  2  1  1  1  1  2  2  12  001100
1  2  1  1  1  1  1  2  1  10  001010
2  2  2  1  2  2  2  1  2  14  001110
3  1  2  2  2  1  1  2  1  12  001100
4  1  2  2  1  1  2  1  1  11  001011
5  2  1  1  2  1  1  2  1  11  001011
6  1  2  2  1  1  2  1  2  12  001100
7  2  2  1  2  1  1  1  1  11  001011
8  2  1  2  2  2  2  2  1  14  001110
9  1  2  1  1  1  2  2  2  12  001100

1 Answer 1

2

One way is to use pandas string slicing

df = pd.DataFrame(np.random.randint(1,3,size=(10, 8)), columns=list('ABCDEFGH'))
df['Q']=df.sum(axis=1)
df['R']=df.Q.apply(lambda x: format(int(x), '06b'))
print("Before:\n", df)
df.R = df.R.str.slice(stop=-1) + (1 - df.R.str.slice(start=-1).astype(int)).astype(str)
print("\nAfter:\n", df)

Output:

 Before:
    A  B  C  D  E  F  G  H   Q       R
0  1  1  2  2  1  1  1  2  11  001011
1  2  2  1  2  1  1  1  2  12  001100
2  1  1  2  1  1  1  1  1   9  001001
3  2  1  2  2  1  2  2  1  13  001101
4  1  1  2  2  1  1  1  2  11  001011
5  2  2  2  1  2  2  2  2  15  001111
6  1  2  1  2  2  1  2  1  12  001100
7  1  1  1  1  1  2  2  2  11  001011
8  1  1  1  2  1  2  1  1  10  001010
9  2  2  1  2  2  1  1  1  12  001100

After:
    A  B  C  D  E  F  G  H   Q       R
0  1  1  2  2  1  1  1  2  11  001010
1  2  2  1  2  1  1  1  2  12  001101
2  1  1  2  1  1  1  1  1   9  001000
3  2  1  2  2  1  2  2  1  13  001100
4  1  1  2  2  1  1  1  2  11  001010
5  2  2  2  1  2  2  2  2  15  001110
6  1  2  1  2  2  1  2  1  12  001101
7  1  1  1  1  1  2  2  2  11  001010
8  1  1  1  2  1  2  1  1  10  001011
9  2  2  1  2  2  1  1  1  12  001101
Sign up to request clarification or add additional context in comments.

8 Comments

May I ask, If I want to flip bit only for first 5 rows and the rest values would be same in the new column, how can I modify that last line (string slicing)? I used df.R.loc[0:5].str.slice() but rest values filled with NaN.
Just replace loc with iloc and add to every df.R in the command and it should work
If I get you correctly, df.R= df.R.iloc[0:5].str.slice(stop=-1) + (1 - df.R.iloc[0:5].str.slice(start=-1).astype(int)).astype(str), gives first five values with bit flip and rest 5 with NaN. But I want rest 5 as previous column.
Add to left hand side as well
It works! I am getting expected values though a warning is shown "SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame".
|

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.