Simon Righley
Simon Righley

Reputation: 4969

How can I map True/False to 1/0 in a Pandas DataFrame?

I have a column in python pandas DataFrame that has boolean True/False values, but for further calculations I need 1/0 representation. Is there a quick pandas/numpy way to do that?

Upvotes: 318

Views: 439220

Answers (14)

Lucas Basquerotto
Lucas Basquerotto

Reputation: 8041

If you want to convert all boolean columns to int, no matter which columns they are, you can select those columns (based on the type) and convert the type:

# Identify boolean columns and convert them to integers
boolean_columns = df.select_dtypes(include='bool').columns
df[boolean_columns] = df[boolean_columns].astype(int)

Upvotes: 1

J Rwar
J Rwar

Reputation: 1

True % (an odd number) = 1 False % (an odd number) = 0

Upvotes: -1

Shashank kumar
Shashank kumar

Reputation: 93

Most efficient way to convert True/False values to 1/0 in a Pandas DataFrame is to use the pd.Series.view() method. This method creates a new NumPy array that shares the memory with the original DataFrame column, but with a different data type. Here's an example:

import pandas as pd

# create a sample DataFrame with True/False values
df = pd.DataFrame({'A': [True, False, True], 'B': [False, True, False]})

# convert True/False values to 1/0 using view()
df['A'] = df['A'].view('i1')
df['B'] = df['B'].view('i1')

# print the resulting DataFrame
print(df)

Upvotes: 0

YScharf
YScharf

Reputation: 2012

Tried and tested:

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

If there are more than one columns with True/False, use the following.

for col in bool_cols:
    df[col] = df[col].map({'True': 1,'False' :0 })

@AMC wrote this in a comment

Upvotes: 2

sanjyay
sanjyay

Reputation: 76

If the column is of the type object, and for example you want to convert it to integer:

df["somecolumn"] = df["somecolumn"].astype(bool).astype(int)

Upvotes: 3

SultanOrazbayev
SultanOrazbayev

Reputation: 16551

This is a reproducible example based on some of the existing answers:

import pandas as pd


def bool_to_int(s: pd.Series) -> pd.Series:
    """Convert the boolean to binary representation, maintain NaN values."""
    return s.replace({True: 1, False: 0})


# generate a random dataframe
df = pd.DataFrame({"a": range(10), "b": range(10, 0, -1)}).assign(
    a_bool=lambda df: df["a"] > 5,
    b_bool=lambda df: df["b"] % 2 == 0,
)

# select all bool columns (or specify which cols to use)
bool_cols = [c for c, d in df.dtypes.items() if d == "bool"]

# apply the new coding to a new dataframe (or can replace the existing one)
df_new = df.assign(**{c: lambda df: df[c].pipe(bool_to_int) for c in bool_cols})

Upvotes: 0

Mike Trotta
Mike Trotta

Reputation: 686

This question specifically mentions a single column, so the currently accepted answer works. However, it doesn't generalize to multiple columns. For those interested in a general solution, use the following:

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

This works for a DataFrame that contains columns of many different types, regardless of how many are boolean.

Upvotes: 61

kaishu
kaishu

Reputation: 59

I had to map FAKE/REAL to 0/1 but couldn't find proper answer.

Please find below how to map column name 'type' which has values FAKE/REAL to 0/1
(Note: similar can be applied to any column name and values)

df.loc[df['type'] == 'FAKE', 'type'] = 0
df.loc[df['type'] == 'REAL', 'type'] = 1

Upvotes: 2

jezrael
jezrael

Reputation: 862511

Use Series.view for convert boolean to integers:

df["somecolumn"] = df["somecolumn"].view('i1')

Upvotes: 4

User
User

Reputation: 65941

A succinct way to convert a single column of boolean values to a column of integers 1 or 0:

df["somecolumn"] = df["somecolumn"].astype(int)

Upvotes: 573

Bruno Benevides
Bruno Benevides

Reputation: 47

You can use a transformation for your data frame:

df = pd.DataFrame(my_data condition)

transforming True/False in 1/0

df = df*1

Upvotes: 2

shubhamgoel27
shubhamgoel27

Reputation: 1439

Just multiply your Dataframe by 1 (int)

[1]: data = pd.DataFrame([[True, False, True], [False, False, True]])
[2]: print data
          0      1     2
     0   True  False  True
     1   False False  True

[3]: print data*1
         0  1  2
     0   1  0  1
     1   0  0  1

Upvotes: 98

Jeff
Jeff

Reputation: 128928

You also can do this directly on Frames

In [104]: df = DataFrame(dict(A = True, B = False),index=range(3))

In [105]: df
Out[105]: 
      A      B
0  True  False
1  True  False
2  True  False

In [106]: df.dtypes
Out[106]: 
A    bool
B    bool
dtype: object

In [107]: df.astype(int)
Out[107]: 
   A  B
0  1  0
1  1  0
2  1  0

In [108]: df.astype(int).dtypes
Out[108]: 
A    int64
B    int64
dtype: object

Upvotes: 23

Gareth Latty
Gareth Latty

Reputation: 88977

True is 1 in Python, and likewise False is 0*:

>>> True == 1
True
>>> False == 0
True

You should be able to perform any operations you want on them by just treating them as though they were numbers, as they are numbers:

>>> issubclass(bool, int)
True
>>> True * 5
5

So to answer your question, no work necessary - you already have what you are looking for.

* Note I use is as an English word, not the Python keyword is - True will not be the same object as any random 1.

Upvotes: 49

Related Questions