是否有一种简单的方法可以将Pandas数据框中的yes/no列更改为1/0?
问题描述:
我将csv文件读入pandas数据帧,并希望将具有二进制答案的列从yes/no字符串转换为1/0的整数.在下面,我显示了其中一列("sampleDF"是熊猫数据框).
I read a csv file into a pandas dataframe, and would like to convert the columns with binary answers from strings of yes/no to integers of 1/0. Below, I show one of such columns ("sampleDF" is the pandas dataframe).
In [13]: sampleDF.housing[0:10]
Out[13]:
0 no
1 no
2 yes
3 no
4 no
5 no
6 no
7 no
8 yes
9 yes
Name: housing, dtype: object
非常感谢您的帮助!
答
方法1
method 1
sample.housing.eq('yes').mul(1)
方法2
method 2
pd.Series(np.where(sample.housing.values == 'yes', 1, 0),
sample.index)
方法3
method 3
sample.housing.map(dict(yes=1, no=0))
方法4
method 4
pd.Series(map(lambda x: dict(yes=1, no=0)[x],
sample.housing.values.tolist()), sample.index)
方法5
method 5
pd.Series(np.searchsorted(['no', 'yes'], sample.housing.values), sample.index)
所有产量
All yield
0 0
1 0
2 1
3 0
4 0
5 0
6 0
7 0
8 1
9 1
定时
给定样本
timing
given sample
定时
长样本sample = pd.DataFrame(dict(housing=np.random.choice(('yes', 'no'), size=100000)))
timing
long samplesample = pd.DataFrame(dict(housing=np.random.choice(('yes', 'no'), size=100000)))