pandas 在熊猫数据框中将“否”和“是”转换为 0 和 1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51672709/
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
Converting 'no' and 'yes' into 0 and 1 in pandas dataframe
提问by Aptha Gowda
I want to convert data of 'edjefe' column which contains int as well as 'yes' and 'no' values. My problem is I just want to map 'yes' and 'no' to 1 and 0 and keep the int values as it is So I wrote this code
我想转换包含 int 以及“yes”和“no”值的“edjefe”列的数据。我的问题是我只想将 'yes' 和 'no' 映射到 1 和 0 并保持 int 值不变所以我写了这段代码
def foo(x):
if x == 'no':
return 0
elif x == 'yes':
return 1
else:
return x
and df1.edjefe.map(lambda x : foo(x))
和 df1.edjefe.map(lambda x : foo(x))
But I am getting an error as,
但我收到一个错误,因为
RecursionError: maximum recursion depth exceeded while calling a Python object
回答by ksbg
You can also just use replace
:
你也可以只使用replace
:
df.edjefe.replace(to_replace=['no', 'yes'], value=[0, 1])
df.edjefe.replace(to_replace=['no', 'yes'], value=[0, 1])
回答by jpp
You can use pd.Series.map
with a dictionary mapping followed by pd.Series.fillna
:
您可以使用pd.Series.map
后跟字典映射pd.Series.fillna
:
d = {'no': 0, 'yes': 1}
df1['edjefe'] = df1['edjefe'].map(d).fillna(df1['edjefe'])
You will likely find this more efficient than pd.Series.replace
.
您可能会发现这比pd.Series.replace
.
See Replace values in a pandas series via dictionary efficientlyfor more details.
有关更多详细信息,请参阅通过字典有效地替换Pandas系列中的值。
If you have mutable objects in your series, this will fail, since dictionary keys must be hashable. You can convert to strings in this case:
如果您的系列中有可变对象,这将失败,因为字典键必须是可散列的。在这种情况下,您可以转换为字符串:
df1['edjefe'] = df1['edjefe'].astype(str).map(d).fillna(df1['edjefe'])
回答by Lev Zakharov
Just use dict-like to_replace
:
只需使用 dict-like to_replace
:
df['edjefe'].replace({'no': 0, 'yes': 1})
回答by ?enol Kurt
You can also try:
你也可以试试:
df1['edjefe'] = (df1['edjefe']=="yes")*1