pythonpandasdataframenumpyboolean

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?


Solution

  • 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)