简体   繁体   English

Pandas 在从 csv 文件读取列时出错

[英]Pandas is making an error while reading a column from a csv file

So I have a google stock dataset that has a 'Date', 'Open', 'High', 'Low', 'Close' and 'Volume' columns.所以我有一个谷歌股票数据集,它有一个“日期”、“开盘”、“高”、“低”、“收盘”和“成交量”列。 The problem is that pandas keeps saying that the 'Close' column is of type 'Object'.问题是 pandas 一直说“关闭”列是“对象”类型。

I found the problem when I tried to scale that column using the MinMaxScaler and that's when I got this error: could not convert string to float: '1,008.64'当我尝试使用MinMaxScaler缩放该列时,我发现了问题,这时我得到了这个错误: could not convert string to float: '1,008.64'

When I tried converting it to a float using the astype('float') function, I get the same error.当我尝试使用astype('float') function 将其转换为浮点数时,我得到了同样的错误。 When I try the to_numeric(price['Close'], errors='coerce') function, it just sets some rows as NULL.当我尝试to_numeric(price['Close'], errors='coerce') function 时,它只是将一些行设置为 NULL。

My code:我的代码:

import pandas as pd

train_data = pd.read_csv('drive/MyDrive/Data LSTM TimeSeries/Google_Stock_Price.csv')

price = train_data[['Close']]
price.info()

from sklearn.preprocessing import MinMaxScaler

scaler = MinMaxScaler(feature_range=(-1,1))
price['Close'] = scaler.fit_transform(price['Close'].values.reshape(-1,1))

price['Close'] = price['Close'].astype('float')
price['Close'] = pd.to_numeric(price['Close'], errors='ignore')

Try using the thousands argument when you use read_csv to make sure that your values are read correctly with the , delimiter在使用read_csv时尝试使用thousands参数,以确保使用,分隔符正确读取您的值

train_data = pd.read_csv('drive/MyDrive/Data LSTM TimeSeries/Google_Stock_Price.csv', thousands = ",")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM