您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Pandas作为字符串而不是布尔值映射到TRUE / FALSE

Pandas作为字符串而不是布尔值映射到TRUE / FALSE

booleandf = pandasDF.select_dtypes(include=[bool])
booleanDictionary = {True: 'TRUE', False: 'FALSE'}

for column in booleandf:
    pandasDF[column] = pandasDF[column].map(booleanDictionary)

pandasDF = pd.DataFrame({'A':[True,False,True],
                   'B':[4,5,6],
                   'C':[False,True,False]})

print (pandasDF)
       A  B      C
0   True  4  False
1  False  5   True
2   True  6  False

booleandf = pandasDF.select_dtypes(include=[bool])
booleanDictionary = {True: 'TRUE', False: 'FALSE'}

#loop by df is loop by columns, same as for column in booleandf.columns:
for column in booleandf:
    pandasDF[column] = pandasDF[column].map(booleanDictionary)

print (pandasDF)
       A  B      C
0   TRUE  4  FALSE
1  FALSE  5   TRUE
2   TRUE  6  FALSE

booleanDictionary = {True: 'TRUE', False: 'FALSE'}
pandasDF = pandasDF.replace(booleanDictionary)
print (pandasDF)
       A  B      C
0   TRUE  4  FALSE
1  FALSE  5   TRUE
2   TRUE  6  FALSE
其他 2022/1/1 18:40:37 有433人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶