Python 有没有一种简单的方法可以将 Pandas 数据框中的一列是/否更改为 1/0?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40901770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:10:35  来源:igfitidea点击:

Is there a simple way to change a column of yes/no to 1/0 in a Pandas dataframe?

pythonpandasdataframeseries

提问by Mushu909

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

我将一个 csv 文件读入一个 Pandas 数据帧,并希望将带有二进制答案的列从是/否字符串转换为 1/0 的整数。下面,我展示了其中一列(“sampleDF”是熊猫数据框)。

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

Help is much appreciated!

非常感谢帮助!

回答by piRSquared

method 1

方法一

sample.housing.eq('yes').mul(1)

method 2

方法二

pd.Series(np.where(sample.housing.values == 'yes', 1, 0),
          sample.index)

method 3

方法三

sample.housing.map(dict(yes=1, no=0))

method 4

方法四

pd.Series(map(lambda x: dict(yes=1, no=0)[x],
              sample.housing.values.tolist()), sample.index)

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

定时
给定样本

enter image description here

在此处输入图片说明

timing
long sample
sample = pd.DataFrame(dict(housing=np.random.choice(('yes', 'no'), size=100000)))

定时
长样本
sample = pd.DataFrame(dict(housing=np.random.choice(('yes', 'no'), size=100000)))

enter image description here

在此处输入图片说明

回答by gold_cy

Try this:

尝试这个:

sampleDF['housing'] = sampleDF['housing'].map({'yes': 1, 'no': 0})

回答by 3novak

# produces True/False
sampleDF['housing'] = sampleDF['housing'] == 'yes'

The above returns True/False values which are essentially 1/0, respectively. Booleans support sum functions, etc. If you really need it to be 1/0 values, you can use the following.

上面分别返回 True/False 值,它们基本上是 1/0。Booleans 支持 sum 函数等,如果你真的需要它是 1/0 值,你可以使用下面的。

housing_map = {'yes': 1, 'no': 0}
sampleDF['housing'] = sampleDF['housing'].map(housing_map)

回答by SriramKRaju

%timeit
sampleDF['housing'] = sampleDF['housing'].apply(lambda x: 0 if x=='no' else 1)

1.84 ms ± 56.2 μs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

每个循环 1.84 ms ± 56.2 μs(7 次运行的平均值 ± 标准偏差,每次 1000 次循环)

Replaces 'yes' with 1, 'no' with 0 for the df column specified.

对于指定的 df 列,将“是”替换为 1,将“否”替换为 0。

回答by praveen kumar

yes there is you can change yes/no values of your column to 1/0 by using following code snippet

是的,您可以使用以下代码片段将列的是/否值更改为 1/0

sampleDF = sampleDF.replace(to_replace = ['yes','no'],value = ['1','0'])
sampleDF

by using first line you can replace the values with 1/0 by using second line you can see the changes by printing it

通过使用第一行,您可以使用第二行将值替换为 1/0,您可以通过打印来查看更改

回答by Siddaram H

Generic way:

通用方式:

import pandas as pd
string_data = string_data.astype('category')
numbers_data = string_data.cat.codes

reference: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.astype.html

参考:https: //pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.astype.html

回答by jpp

You can convert a series from Boolean to integer explicitly:

您可以显式地将系列从布尔值转换为整数:

sampleDF['housing'] = sampleDF['housing'].eq('yes').astype(int)

回答by Eslamspot

The easy way to do that use pandas as below:

使用熊猫的简单方法如下:

housing = pd.get_dummies(sampleDF['housing'],drop_first=True)

after that drop this filed from main df

之后从主 df 中删除此文件

sampleDF.drop('housing',axis=1,inplace=True)

now merge new one in you df

现在在你 df 中合并新的

sampleDF= pd.concat([sampleDF,housing ],axis=1)

回答by Freek Nortier

Use sklearn's LabelEncoder

使用 sklearn 的 LabelEncoder

from sklearn.preprocessing import LabelEncoder

lb = LabelEncoder() 
sampleDF['housing'] = lb.fit_transform(sampleDF['housing'])

Source

来源

回答by Josmy Faure

A simple and intuitive way to convert the whole dataframe to 0's and 1's might be:

将整个数据帧转换为 0 和 1 的一种简单直观的方法可能是:

sampleDF = sampleDF.replace(to_replace = "yes", value = 1)
sampleDF = sampleDF.replace(to_replace = "no", value = 0)