【发布时间】:2017-08-04 22:47:24
【问题描述】:
当我尝试将 pandas 数据框中的某些列从“0”和“1”转换为“TRUE”和“FALSE”时,pandas 会自动将 dtype 检测为布尔值。我想将 dtype 保留为字符串,字符串为 'TRUE' 和 'FALSE'。
见下面的代码:
booleanColumns = pandasDF.select_dtypes(include=[bool]).columns.values.tolist()
booleanDictionary = {'1': 'TRUE', '0': 'FALSE'}
pandasDF.to_string(columns = booleanColumns)
for column in booleanColumns:
pandasDF[column].map(booleanDictionary)
不幸的是,python 使用最后一个操作自动将 dtype 转换为 boolean。我怎样才能防止这种情况发生?
【问题讨论】:
标签: python pandas dictionary replace