pd.read_csv gives me str but need float

前端 未结 2 561
感动是毒
感动是毒 2021-01-19 13:25

I have a CSV which looks like this:

Date,Open,High,Low,Close,Adj Close,Volume
2007-07-25,4.929000,4.946000,4.896000,4.904000,4.904000,0
2007-07-26,4.863000,4         


        
相关标签:
2条回答
  • 2021-01-19 14:10

    The read_csv dtype option doesn't work ?

    from the documentation dtype : Type name or dict of column -> type, default None Data type for data or columns. E.g. {‘a’: np.float64, ‘b’: np.int32} Use str or object to preserve and not interpret dtype. If converters are specified, they will be applied INSTEAD of dtype conversion.

    data = pd.read_csv(file,
        index_col='Date',
        usecols=['High','Low'],
        dtype={'High': np.float64, 'Low': np.float64})
    
    0 讨论(0)
  • 2021-01-19 14:18

    I think you need to_numeric with errors='coerce' because it seems there are some bad data:

    data = pd.read_csv(file, index_col='Date', usecols=['High','Low'])
    
    data = data.apply(pd.to_numeric, errors='coerce')
    
    0 讨论(0)
提交回复
热议问题