Python 熊猫将任何值转换为布尔值(缺失或零)

Python 熊猫将任何值转换为布尔值(缺失或零),python,pandas,dataframe,casting,Python,Pandas,Dataframe,Casting,在python中,编写类似于 if a: ... 即使a是整数(例如0)或字符串(例如空字符串),在这种情况下,它也将被转换为false 然而 dtype_dict = {'type': str, 'exclude_trial':bool} data = pd.read_csv(filename, sep='\t', dtype=dtype_dict) 返回警告 ValueError: cannot safely convert passed user dtype of bool for o

在python中,编写类似于

if a: ...
即使
a
是整数(例如0)或字符串(例如空字符串),在这种情况下,它也将被转换为false

然而

dtype_dict = {'type': str, 'exclude_trial':bool}
data = pd.read_csv(filename, sep='\t', dtype=dtype_dict)
返回警告

ValueError: cannot safely convert passed user dtype of bool for object dtyped data in column 26
这是完全不清楚的,非常烦人


列“exclude_trial”包含值“”(空字符串)或“0”或“1”。当然,它只能按照正常的行为简单地浇铸,否则也是正常的。(“”,'0'->false,1->true)

读取数据后,您可以在数据帧上使用
.astype(bool)
对其进行转换,使用与
bool(x)
等效的值,其中x是每个单元格中的值

然而,你需要非常小心
pandas
有很多自动NaN处理,这意味着空字符串通常会被读入并强制到NaN,这是有问题的,因为
bool(“”)
False
bool(np.NaN)
True
。即使指定数据类型也不能防止这种情况,但可以使用
na_filter
确保它不会更改空字符串

样本数据:
test.txt

col1,col2,col3
True,1,
False,0,
True,1,1

df = pd.read_csv('test.txt', na_filter=False)
#    col1  col2 col3
#0   True     1     
#1  False     0     
#2   True     1    1

df = df.astype(bool)
#    col1   col2   col3
#0   True   True  False
#1  False  False  False
#2   True   True   True

df.dtypes
#col1    bool
#col2    bool
#col3    bool