0

I would like to change so True = False or more exact change so True = 0 and False = 1 is there a way to do this?

I have a dataframe and would like to df.groupby('country',as_index=False).sum() and see how many False values there is in each country

I have tried df['allowed'] = --df['allowed'] (allowed is the column with True and False values) to swap them but it didn't work

Monsson
  • 7
  • 4

3 Answers3

0

Swapping booleans is easy with df["neg_allowed"] = ~df['allowed']

nthtry
  • 16
  • 2
0
# we can use map method to change values directly 

df['allowed'] = df['allowed'].map({True: 0, False: 1})

#Before:   allowed      --->  #After:     allowed
            True                         0
            False                        1
Bharat Adhikari
  • 334
  • 1
  • 6
0

Example

s1 = pd.Series([True, True, False, False])

output(s1):

0     True
1     True
2    False
3    False
dtype: bool

Code

True -> 0 & False -> 1

1 - s1

result:

0    0
1    0
2    1
3    1
dtype: int32
Panda Kim
  • 6,246
  • 2
  • 12