本文介绍了如何在Pandas DataFrame中将True/False映射到1/0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在python pandas DataFrame中有一个具有布尔True/False值的列,但是为了进行进一步的计算,我需要1/0表示形式.有没有一种快速的熊猫/numpy方法来做到这一点?
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?
推荐答案
一种将布尔值的单列转换为整数1或0的简洁方法:
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)
这篇关于如何在Pandas DataFrame中将True/False映射到1/0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!