替换pandas DataFrame中的列值 [英] Replacing column values in a pandas DataFrame

查看:203
本文介绍了替换pandas DataFrame中的列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试替换数据框的一列中的值.列("female")仅包含值"female"和"male".

I'm trying to replace the values in one column of a dataframe. The column ('female') only contains the values 'female' and 'male'.

我尝试了以下操作:

w['female']['female']='1'
w['female']['male']='0' 

但是会收到与以前结果完全相同的副本.

But receive the exact same copy of the previous results.

理想情况下,我希望获得一些类似于以下循环元素的输出.

I would ideally like to get some output which resembles the following loop element-wise.

if w['female'] =='female':
    w['female'] = '1';
else:
    w['female'] = '0';

我已经仔细阅读了gotchas文档( http://pandas.pydata.org/pandas- docs/stable/gotchas.html ),但无法弄清为什么什么也没发生.

I've looked through the gotchas documentation (http://pandas.pydata.org/pandas-docs/stable/gotchas.html) but cannot figure out why nothing happens.

任何帮助将不胜感激.

推荐答案

如果我理解正确,那么您需要这样的东西:

If I understand right, you want something like this:

w['female'] = w['female'].map({'female': 1, 'male': 0})

(在这里,我将值转换为数字,而不是包含数字的字符串.如果确实需要,可以将它们转换为"1""0",但是我不确定为什么要这么做.)

(Here I convert the values to numbers instead of strings containing numbers. You can convert them to "1" and "0", if you really want, but I'm not sure why you'd want that.)

您的代码不起作用的原因是因为在列(w['female']['female']中的第二个'female')上使用['female']并不意味着选择值是'female'的行".这意味着选择 index 为女性"的行,而您的DataFrame中可能没有该行.

The reason your code doesn't work is because using ['female'] on a column (the second 'female' in your w['female']['female']) doesn't mean "select rows where the value is 'female'". It means to select rows where the index is 'female', of which there may not be any in your DataFrame.

这篇关于替换pandas DataFrame中的列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆