如何仅将 dtype bool 列的 Pandas 数据框中的 True 和 False 映射到“是”和“否”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45196626/
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-09-14 04:02:27  来源:igfitidea点击:

How to map True and False to 'Yes' and 'No' in a pandas data frame for columns of dtype bool only?

pythonpandasdataframereplaceboolean

提问by Alex Tereshenkov

I have a pandasdata frame (v 0.20.3):

我有一个pandas数据框(v 0.20.3):

df = pd.DataFrame({'coname1': ['Apple','Yahoo'], 'coname2':['Apple', 'Google']})
df['eq'] = df.apply(lambda row: row['coname1'] == row['coname2'], axis=1).astype(bool)

   coname1 coname2     eq
0    Apple   Apple   True
1    Yahoo  Google  False

If I would like to replace True/Falseto 'Yes'/'No', I could run this:

如果我想替换True/False'Yes'/'No',我可以运行这个:

df.replace({
                True: 'Yes',
                False: 'No'
            })

   coname1 coname2   eq
0    Apple   Apple  Yes
1    Yahoo  Google   No

Which seems to get the job done. However, if a data frame is just one row with a value of 0/1in a column, it will be also replaced as it's being treated as Boolean.

这似乎完成了工作。但是,如果数据框只是一列中值为 的一行0/1,它也会被替换,因为它被视为布尔值。

df1 = pd.DataFrame({'coname1': [1], 'coname2':['Google'], 'coname3':[777]})
df1['eq'] = True

   coname1 coname2  coname3    eq
0        1  Google      777  True

df1.replace({
                True: 'Yes',
                False: 'No'
            })

  coname1 coname2 coname3   eq
0     Yes  Google     777  Yes

I would like to map True/Falseto Yes/Nofor all columns in the data frame that are of dtypebool.

我想映射True/FalseYes/No数据框中的所有列dtypebool

How do I tell pandasto run map True/False to arbitrary strings only for the columns that are of dtypeboolwithout explicitly specifying the names of columns as I may not know them in advance?

我如何告诉pandas只为那些dtypebool没有明确指定列名的列运行映射 True/False 到任意字符串,因为我可能事先不知道它们?

回答by ayhan

Use the dtypes attribute to check if the column is boolean and filter based on that:

使用 dtypes 属性检查列是否为布尔值并基于此进行过滤:

df = pd.DataFrame({'A': [0, 1], 'B': ['x', 'y'], 
                   'C': [True, False], 'D': [False, True]})

df
Out: 
   A  B      C      D
0  0  x   True  False
1  1  y  False   True

bool_cols = df.columns[df.dtypes == 'bool']

df[bool_cols] = df[bool_cols].replace({True: 'Yes', False: 'No'})

df
Out: 
   A  B    C    D
0  0  x  Yes   No
1  1  y   No  Yes

I think the fastest way would be to use map in a loop though:

我认为最快的方法是在循环中使用 map :

for col in df.columns[df.dtypes == 'bool']:
    df[col] = df[col].map({True: 'Yes', False: 'No'})

回答by Vinícius Aguiar

A nice workaround is to create a function that first checks if the element is of type bool or not, and then use applymap:

一个很好的解决方法是创建一个函数,首先检查元素是否为 bool 类型,然后使用applymap

import pandas as pd

df1 = pd.DataFrame({'coname1': [1], 'coname2':['Google'], 'coname3':[777]})
df1['eq'] = True

def bool2yes(boolean):
    if isinstance(boolean, bool):
        if boolean == True:
            return "Yes"
        else:
            return "No"
    else:
        return boolean

>>> df1.applymap(bool2yes)
   coname1 coname2  coname3   eq
0        1  Google      777  Yes

回答by piRSquared

My Take

我的看法

cols = df.columns[df.dtypes.eq(bool)]
vals = np.column_stack([df[c].values for c in cols])

df[cols] = np.array(['No', 'Yes'])[vals.astype(int)]

df

   A  B    C    D
0  0  x  Yes   No
1  1  y   No  Yes