pd.read_csv给了我str但需要浮点数 [英] pd.read_csv gives me str but need float
本文介绍了pd.read_csv给了我str但需要浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个看起来像这样的CSV:
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.867000,4.759000,4.777000,4.777000,0
2007-07-27,4.741000,4.818000,4.741000,4.788000,4.788000,0
2007-07-30,4.763000,4.810000,4.763000,4.804000,4.804000,0
之后
data = pd.read_csv(file, index_col='Date').drop(['Open','Close','Adj Close','Volume'], axis=1)
我最终得到一个看起来像这样的df:
i end up with a df which looks like this:
High Low
Date
2007-07-25 4.946000 4.896000
2007-07-26 4.867000 4.759000
2007-07-27 4.818000 4.741000
2007-07-30 4.810000 4.763000
2007-07-31 4.843000 4.769000
现在我想获得高-低.尝试过:
Now i want to get High - Low. Tried:
np.diff(data.values, axis=1)
但出现错误:-:'str'和'str'不支持的操作数类型
but getting an error: unsupported operand type(s) for -: 'str' and 'str'
,但是请确定为什么df中的值首先是str.感谢您提出任何解决方案.
but sure why the values in the df are str in the first place. Grateful for any solution.
推荐答案
我认为您需要 to_numeric
和errors='coerce'
,因为似乎有一些不良数据:
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')
这篇关于pd.read_csv给了我str但需要浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文