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

查看:71
本文介绍了替换 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.)

您的代码不起作用的原因是因为在列上使用了 ['female'](w[' 中的第二个 '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天全站免登陆