[Solved] Pandas–TypeError: ‘bool’ object is not iterable

import pandas as pd
df = pd.read_csv(ori_file_name, names=['img_name','label'])
df.to_csv(new_file_name, index=False, columns= False)

报错:

df.to_csv(new_file_name, index=False, columns= False)
TypeError: 'bool' object is not iterable

Solved

bool 类型的数据不可迭代,就说明采用布尔值的地方出错了,通过查找源代码,发现columns=None 或者列表赋值,因而columns=False 会导致错误,正确代码如下:

import pandas as pd
df = pd.read_csv(ori_file_name, names=['img_name','label'])
df.to_csv(new_file_name, index=False, columns= ['img_name','label'])
#df.to_csv(new_file_name, index=False )  #或者采用默认值

小结:
这种错误的排查有两个思路
1)数据本身问题
2)错误调用

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐