Ashim Sinha
Ashim Sinha

Reputation: 577

How to convert true false values in dataframe as 1 for true and 0 for false

How to convert true false values in Dataframe as 1 for true and 0 for false

COL1  COL2 COL3  COL4
12   TRUE  14    FALSE
13   FALSE  13    TRUE


OUTPUT
12   1  14 0
13   0  13 1

Upvotes: 32

Views: 107161

Answers (12)

Jonathan Koren
Jonathan Koren

Reputation: 933

Use pandas.DataFrame.replace

>>> df

   COL1   COL2  COL3   COL4
0    12   TRUE    14  FALSE
1    13  FALSE    13   TRUE

>>> df.replace(['TRUE','FALSE'],[1,0])

   COL1  COL2  COL3  COL4
0    12     1    14     0
1    13     0    13     1

Upvotes: 2

Pranzell
Pranzell

Reputation: 2485

A simple and clean way to do this is using numpy's where function

df['Y'] = np.where(df['X'] == "SOME_VALUE", 1, 0)

will internally calculate True/False for values and replace them with 1/0 creating a array of ones and zeros.

Similarly,

df['Y'] = np.where((df['X'] == "SOME_VALUE") & (df['Z'] == "SOME_VALUE"), 100, 0)

works like a charm and is quite similar to Ms Excel calculations making it intuitive.

Upvotes: 0

Atharva Kousadikar
Atharva Kousadikar

Reputation: 21

You can try following method:

variable_name = {'True' : 0 , 'False' : 1 }

data['Column_name'] = data['Column_name'].map(Variable_name)

Upvotes: 2

CRAZYDATA
CRAZYDATA

Reputation: 135

This does not work:

df['COL2'] = (df['COL2'] == 'TRUE').astype(int)

This works:

df['COL2'] = (df['COL2'] == True ).astype(int)

Upvotes: 4

Saman
Saman

Reputation: 393

df=pd.DataFrame(data={'col1' : [True, False, True],
                 'col2': [14, 15, 12],
                 'col3': [False, True, True]})
df[['col1', 'col3']]=df[['col1', 'col3']].astype('int')
df
Output:
    col1    col2    col3
0   1        14      0
1   0        15      1
2   1        12      1

Upvotes: 2

Hadi Rasekh
Hadi Rasekh

Reputation: 2740

If you have a categorical column in your data (such as country name) .astype(int) will return an error A better choice is to multiply your data with one

data = pd.read_csv('data.txt', header = None) 
data *= 1 # make true/false -> 1/0
print(data)

so if you have

True False USA
False False USA
True True russia

result will be

1 0 USA
0 0 USA
1 1 USA

Upvotes: 1

james
james

Reputation: 51

Suppose d is the dataframe you want to convert

f = lambda x: 1 if x==True else 0

d.applymap(f) should be what you want.

Upvotes: 5

Anon George
Anon George

Reputation: 910

You can convert the 'True' and 'False' values (strings) to 1 and 0 respectively for a specific column (here we choose 3rd column) as follows.

from pandas import DataFrame as df
data = df(data) # where data contains your data as rows and columns
                # and it is converted to dataframe using pandas (ignore if already df)
for i in range(len(data[3])):
    if data[3][i] == 'TRUE':
        data[3][i] = 1
    elif data[3][i] == 'FALSE':
        data[3][i] = 0
    else:
        pass

This method can be used to compare any value or string and replace that location with the required value or string.

Upvotes: 0

Naveen Reddy
Naveen Reddy

Reputation: 41

You can also try this to convert the boolean values like True or False to 1 or 0.

    In [2] : df['attribute_name']
    Out[2] : 0 True
             1 False
             2 True
             3 True

Now import these packages:

    In [3] : from sklearn import preprocessing
             lab_enc = preprocessing.LabelEncoder()
             lab_enc.fit(df['attribute_name'])
             variable = lab_enc.transform(df['attribute_name'])
             df['variable'] = variable
             print df['variable']
    Out[4] : 0 1
             1 0
             2 1
             3 1

If you want to revert back the values from 0 or 1 to False or True you can use lab_encoder.inverse_transform([0,1]) which results the output from 0 or 1 to False or True

Upvotes: 0

Simply doing this:

df[["col2", "col4"]] *= 1

Python considers boolean values (True, False) like (1, 0) respectively. So you can operate with it like numbers.

Upvotes: 20

abarnert
abarnert

Reputation: 365637

First, if you have the strings 'TRUE' and 'FALSE', you can convert those to boolean True and False values like this:

df['COL2'] == 'TRUE'

That gives you a bool column. You can use astype to convert to int (because bool is an integral type, where True means 1 and False means 0, which is exactly what you want):

(df['COL2'] == 'TRUE').astype(int)

To replace the old string column with this new int column, just assign it:

df['COL2'] = (df['COL2'] == 'TRUE').astype(int)

And to do that to two columns at one, just index with a list of columns:

df[['COL2', 'COL4']] = (df[['COL2', 'COL4']] == 'TRUE').astype(int)

Upvotes: 43

Zero
Zero

Reputation: 76917

You could convert the type of each column like

In [7]: df[['COL2', 'COL4']] = df[['COL2', 'COL4']].astype(int)

In [8]: df
Out[8]:
   COL1  COL2  COL3  COL4
0    12     1    14     0
1    13     0    13     1

Even df[['COL2', 'COL4']].astype(float) works for conversion.

Upvotes: 5

Related Questions