Robbie
Robbie

Reputation: 275

Replacing integers of all columns of a pandas dataframe with True/False

I've got a pandas data frame with integers.

I want to replace 1 with True and 2 with False for all columns in my data frame , so to make a data frame with booleans only.

I thought I'd try to loop over all of the columns using something like df[Col] = df[Col].astype(bool), but when I tried it out on Col1 it just replaced every value in Col1 with True when it should be True, True, False, True.

Thanks for any help!

import pandas as pd

data = {'Col1': [1,1,2,1],
        'Col2': [2,2,1,1],
        'Col3': [1,1,1,2],
        'Col4': [2,1,2,2]
        }

df = pd.DataFrame(data, columns = ['Col1', 'Col2', 'Col3', 'Col4'])

Upvotes: 1

Views: 785

Answers (5)

Balaji Ambresh
Balaji Ambresh

Reputation: 5037

Here you go:

>>> df == 1                      
    Col1   Col2   Col3   Col4
0   True  False   True  False
1   True  False   True   True
2  False   True   True  False
3   True   True  False  False

Upvotes: 1

Ch3steR
Ch3steR

Reputation: 20669

You can use df.replace.

df.replace([1,2],[True,False])

Upvotes: 1

Scott Boston
Scott Boston

Reputation: 153460

Just use:

df == 1

Output:

    Col1   Col2   Col3   Col4
0   True  False   True  False
1   True  False   True   True
2  False   True   True  False
3   True   True  False  False

Upvotes: 4

Bhaskar
Bhaskar

Reputation: 700

The problem is that in the context of integers, .astype(bool) changes all non-zero integers to True, and all zeros to False. I would use map as follows:

df[Col] = df[Col].map({1:True, 2:False})

Upvotes: 0

David Erickson
David Erickson

Reputation: 16683

import pandas as pd

data = {'Col1': [1,1,2,1],
        'Col2': [2,2,1,1],
        'Col3': [1,1,1,2],
        'Col4': [2,1,2,2]
        }

df = pd.DataFrame(data, columns = ['Col1', 'Col2', 'Col3', 'Col4'])
df = df.replace(1,True)
df = df.replace(2,False)
df

Output:

    Col1    Col2    Col3    Col4
0   True    False   True    False
1   True    False   True    True
2   False   True    True    False
3   True    True    False   False

Upvotes: 1

Related Questions