Pandas read_csv, reading a boolean with missing values specified as an int

只愿长相守 提交于 2021-01-27 07:10:32

问题


I am trying to import a csv into a pandas dataframe. I have boolean variables denoted with 1's and 0's, where missing values are identified with a -9. When I try to specify the dtype as boolean, I get a host of different errors, depending on what I try.

Sample data: test.csv

var1, var2
0,   0
0,   1
1,   3
-9,  0
0,   2
1,   7

I try to specify the dtype as I import:

dtype_dict = {'var1':'bool','var2':'int'}
nan_dict = {'var1':[-9]}
foo = pd.read_csv('test.csv',dtype=dtype_dict, na_values=nan_dict)

I get the following error:

ValueError: cannot safely convert passed user dtype of |b1 for int64 dtyped data in column 0

I have also tried specifying the true and false values,

foo = pd.read_csv('test.csv',dtype=dtype_dict,na_values=nan_dict,
                 true_values=[1],false_values=[0])

but then I get a different error:

Exception: Must be all encoded bytes

The source code for the error says something about catching the occasional none, but nones or nulls are exactly what I want.


回答1:


You can specify the converters parameter for the var1 column:

from io import StringIO
import numpy as np
import pandas as pd

pd.read_csv(StringIO("""var1, var2
0,   0
0,   1
1,   3
-9,  0
0,   2
1,   7"""), converters = {'var1': lambda x: bool(int(x)) if x != '-9' else np.nan})




回答2:


Can you do something like this?

df=pd.read_csv("test.csv",names=["var1","var2"])
df.ix[df.var1==0,'var1Bool']=False
df.ix[df.var1==1,'var1Bool']=True

Thi should create you a new column and if you are satisfied you can just copy over the old one.

   var1  var2 var1Bool
0     0     0    False
1     0     1    False
2     1     3     True
3    -9     0      NaN
4     0     2    False
5     1     7     True


来源:https://stackoverflow.com/questions/41304332/pandas-read-csv-reading-a-boolean-with-missing-values-specified-as-an-int

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!