如何在Pandas DataFrame中将True / False映射到1/0?

问题:如何在Pandas DataFrame中将True / False映射到1/0?

我在python pandas DataFrame中有一列具有布尔True / False值的列,但是对于进一步的计算,我需要1/0表示形式。有没有一种快速的方法来做到这一点?

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?


回答 0

一种将布尔值的单列转换为整数1或0的列的简洁方法:

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

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)

回答 1

只需将您的数据框乘以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

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

回答 2

True1在Python,同样False0*

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

通过将它们视为数字,就可以对它们执行所需的任何操作,因为它们数字:

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

因此,回答您的问题,无需任何工作-您已经有了所需的东西。

*请注意,我使用的英文单词,而不是Python关键字isTrue与任何random都不是同一对象1

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 isTrue will not be the same object as any random 1.


回答 3

您也可以直接在框架上执行此操作

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

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

回答 4

您可以对数据框使用转换:

df = pd.DataFrame(my_data condition)

在1/0中转换真/假

df = df*1

You can use a transformation for your data frame:

df = pd.DataFrame(my_data condition)

transforming True/False in 1/0

df = df*1

回答 5

使用Series.view的转换布尔为整数:

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

Use Series.view for convert boolean to integers:

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