大熊猫作为字符串而不是布尔值映射到TRUEFALSE

大熊猫作为字符串而不是布尔值映射到TRUEFALSE,第1张

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

如果需要,请替换

boolean
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      C0   True  4  False1  False  5   True2   True  6  Falsebooleandf = 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      C0   TRUE  4  FALSE1  FALSE  5   TRUE2   TRUE  6  FALSE

编辑:

与Simplier解决方案

replace
通过
dict

booleanDictionary = {True: 'TRUE', False: 'FALSE'}pandasDF = pandasDF.replace(booleanDictionary)print (pandasDF)       A  B      C0   TRUE  4  FALSE1  FALSE  5   TRUE2   TRUE  6  FALSE


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5617505.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-15
下一篇2022-12-15

发表评论

登录后才能评论

评论列表(0条)

    保存