How to swap values ​​0 and 1 on each other in a pandas data frame?

I am working with the pandas framework, which has a column of all 0 and 1, and I'm trying to switch each of the values ​​(i.e. all 0 become 1 and all 1 become 0). Is there an easy way to do this?

+4
source share
3 answers

Use replace:

df = df.replace({0:1, 1:0})

Or faster numpy.logical_xor:

df = np.logical_xor(df,1).astype(int)

Or faster:

df = pd.DataFrame(np.logical_xor(df.values,1).astype(int),columns=df.columns, index=df.index)

Example:

np.random.seed(12)
df = pd.DataFrame(np.random.choice([0,1], size=[10,3]))
print (df)
   0  1  2
0  1  1  0
1  1  1  0
2  1  1  0
3  0  0  1
4  0  1  1
5  1  0  1
6  0  0  0
7  1  0  0
8  1  0  1
9  1  0  0

df = df.replace({0:1, 1:0})
print (df)
   0  1  2
0  0  0  1
1  0  0  1
2  0  0  1
3  1  1  0
4  1  0  0
5  0  1  0
6  1  1  1
7  0  1  1
8  0  1  0
9  0  1  1

Another solution:

df = (~df.astype(bool)).astype(int)
print (df)
   0  1  2
0  0  0  1
1  0  0  1
2  0  0  1
3  1  1  0
4  1  0  0
5  0  1  0
6  1  1  1
7  0  1  1
8  0  1  0
9  0  1  1

Delay

np.random.seed(12)
df = pd.DataFrame(np.random.choice([0,1], size=[10000,10000]))
print (df)

In [69]: %timeit (np.logical_xor(df,1).astype(int))
1 loop, best of 3: 1.42 s per loop

In [70]: %timeit (df ^ 1)
1 loop, best of 3: 2.53 s per loop

In [71]: %timeit ((~df.astype(bool)).astype(int))
1 loop, best of 3: 1.81 s per loop

In [72]: %timeit (df.replace({0:1, 1:0}))
1 loop, best of 3: 5.08 s per loop

In [73]: %timeit pd.DataFrame(np.logical_xor(df.values,1).astype(int), columns=df.columns, index=df.index)
1 loop, best of 3: 350 ms per loop

Edit: This should be faster:

import numexpr as ne
arr = df.values
df = pd.DataFrame(ne.evaluate('1 - arr'),columns=df.columns, index=df.index)
+4
source

If your framework consists of only 1 with 0, you can use the XOR operator df ^ 1

In [19]: import pandas as pd

In [20]: df = pd.DataFrame({"a": [1,0,1], "b": [0,1,1]})

In [21]: df
Out[21]: 
   a  b
0  1  0
1  0  1
2  1  1

In [22]: df ^ 1
Out[22]: 
   a  b
0  0  1
1  1  0
2  0  0
+2
source

-

df[:] = 1-df.values

, , :

a = df.values
a[:] = 1-a

-

In [43]: df
Out[43]: 
   0  1  2
0  0  0  1
1  0  0  1
2  0  0  1
3  1  1  0
4  1  0  0

In [44]: df[:] = 1-df.values

In [45]: df
Out[45]: 
   0  1  2
0  1  1  0
1  1  1  0
2  1  1  0
3  0  0  1
4  0  1  1

@jezrael timings setup , ,

In [46]: np.random.seed(12)
    ...: df = pd.DataFrame(np.random.choice([0,1], size=[10000,10000]))
    ...: 

# Proposed in this post
In [47]: def swap_0_1(df):
    ...:     a = df.values
    ...:     a[:] = 1-a
    ...:     

In [48]: %timeit pd.DataFrame(np.logical_xor(df.values,1).astype(int), columns=df.columns, index=df.index)
10 loops, best of 3: 218 ms per loop

In [49]: %timeit swap_0_1(df)
10 loops, best of 3: 198 ms per loop

-

In [60]: def swap_0_1_bool(df):
    ...:     a = df.values
    ...:     a[:] = ~a.astype(bool)
    ...:     

In [63]: %timeit swap_0_1_bool(df)
10 loops, best of 3: 179 ms per loop
+2
source

Source: https://habr.com/ru/post/1681423/


All Articles