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

痞子三分冷 提交于 2020-11-28 04:54:13

问题


I have a pandas data frame (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/False to 'Yes'/'No', I could run this:

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/1 in a column, it will be also replaced as it's being treated as Boolean.

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/False to Yes/No for all columns in the data frame that are of dtype bool.

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


回答1:


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

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:

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



回答2:


A nice workaround is to create a function that first checks if the element is of type bool or not, and then use 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



回答3:


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


来源:https://stackoverflow.com/questions/45196626/how-to-map-true-and-false-to-yes-and-no-in-a-pandas-data-frame-for-columns-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!