我试图替换一个数据框架的一列的值。列('female')只包含值'female'和'male'。

我尝试过以下方法:

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

但收到的是与之前结果完全相同的副本。

理想情况下,我希望得到类似于以下循环元素的输出。

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

我已经查看了gotchas文档(http://pandas.pydata.org/pandas-docs/stable/gotchas.html),但不明白为什么什么都没有发生。

任何帮助都将不胜感激。


当前回答

还有一个内置函数pd。Get_dummies用于这些类型的赋值:

w['female'] = pd.get_dummies(w['female'],drop_first = True)

这为您提供了一个有两列的数据帧,每一列对应出现在w['female']中的每个值,您可以删除其中的第一列(因为您可以从剩下的一列推断它)。新列将自动命名为您替换的字符串。

如果有两个以上可能值的分类变量,这尤其有用。这个函数创建了尽可能多的虚拟变量来区分所有情况。请注意,不要将整个数据帧分配到单个列中,相反,如果w['female']可以是'male', 'female'或'neutral',请执行如下操作:

w = pd.concat([w, pd.get_dummies(w['female'], drop_first = True)], axis = 1])
w.drop('female', axis = 1, inplace = True)

然后你剩下两个新的列,给你'female'的虚拟编码,你去掉了带字符串的列。

其他回答

w.replace({'female':{'female':1, 'male':0}}, inplace = True)

上面的代码将把'female'替换为1,'male'替换为0,仅在'female'列中

轻微的变化:

w.female.replace(['male', 'female'], [1, 0], inplace=True)

你也可以使用apply和。get i.e.。

"菲玛"等于"菲玛"克里奥耳语(——“画”:0,可说是家常便饭”:1,.get):

w = pd.DataFrame({'female':['female','male','female']})
print(w)

Dataframe w:

   female
0  female
1    male
2  female

使用apply替换字典中的值:

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

结果:

   female
0       1
1       0
2       1 

注意:如果数据帧中所有可能的列的值都是在字典else中定义的,那么apply with dictionary应该被使用,对于那些没有在字典中定义的列,它将为空。

w.female = np.where(w.female=='female', 1, 0)

如果有人在寻找一个麻木的解决方案。这对于基于条件替换值非常有用。if和else条件都是np.where()中固有的。使用df.replace()的解决方案可能是不可行的,如果列包含除'male'之外的许多唯一值,所有这些值都应该替换为0。

另一种解决方案是连续使用df.where()和df.mask()。这是因为它们都没有实现else条件。

w.female.where(w.female=='female', 0, inplace=True) # replace where condition is False
w.female.mask(w.female=='female', 1, inplace=True) # replace where condition is True

为了更一般地回答这个问题,使它适用于更多的用例,而不仅仅是OP要求的用例,可以考虑这个解决方案。我使用jfs的解决方案来帮助我。在这里,我们创建了两个相互帮助的函数,无论您是否知道确切的替换都可以使用它们。

import numpy as np
import pandas as pd


class Utility:

    @staticmethod
    def rename_values_in_column(column: pd.Series, name_changes: dict = None) -> pd.Series:
        """
        Renames the distinct names in a column. If no dictionary is provided for the exact name changes, it will default
        to <column_name>_count. Ex. female_1, female_2, etc.

        :param column: The column in your dataframe you would like to alter.
        :param name_changes: A dictionary of the old values to the new values you would like to change.
        Ex. {1234: "User A"} This would change all occurrences of 1234 to the string "User A" and leave the other values as they were.
        By default, this is an empty dictionary.
        :return: The same column with the replaced values
        """
        name_changes = name_changes if name_changes else {}
        new_column = column.replace(to_replace=name_changes)
        return new_column

    @staticmethod
    def create_unique_values_for_column(column: pd.Series, except_values: list = None) -> dict:
        """
        Creates a dictionary where the key is the existing column item and the value is the new item to replace it.
        The returned dictionary can then be passed the pandas rename function to rename all the distinct values in a
        column.
        Ex. column ["statement"]["I", "am", "old"] would return
        {"I": "statement_1", "am": "statement_2", "old": "statement_3"}

        If you would like a value to remain the same, enter the values you would like to stay in the except_values.
        Ex. except_values = ["I", "am"]
        column ["statement"]["I", "am", "old"] would return
        {"old", "statement_3"}

        :param column: A pandas Series for the column with the values to replace.
        :param except_values: A list of values you do not want to have changed.
        :return: A dictionary that maps the old values their respective new values.
        """
        except_values = except_values if except_values else []
        column_name = column.name
        distinct_values = np.unique(column)
        name_mappings = {}
        count = 1
        for value in distinct_values:
            if value not in except_values:
                name_mappings[value] = f"{column_name}_{count}"
                count += 1
        return name_mappings

对于OP的用例,使用它非常简单

w["female"] = Utility.rename_values_in_column(w["female"], name_changes = {"female": 0, "male":1}

然而,要知道您想要重命名的数据帧中所有不同的惟一值并不总是那么容易。在我的例子中,列的字符串值是散列值,因此它们损害了可读性。我所做的是使用create_unique_values_for_column函数将这些散列值替换为更可读的字符串。

df["user"] = Utility.rename_values_in_column(
    df["user"],
    Utility.create_unique_values_for_column(df["user"])
)

这将改变我的用户列值从["1a2b3c", "a12b3c","1a2b3c"]到["user_1", "user_2", "user_1]。比较起来容易多了,对吧?