【问题标题】:pandas read_csv issue with reading large filespandas read_csv 读取大文件的问题
【发布时间】:2020-04-03 22:43:36
【问题描述】:

我正在尝试构建一个项目并创建一个包含 13347 行和 2500 列的 csv,但在通过 pandas 读取文件期间,仅读取了初始 6600 个值,因此我的模型构建不正确。请告诉我为什么会发生这种情况以及我该如何解决。我正在附加一部分带有输出的代码。

**Code:**
data=pd.read_csv("train_foo.csv",low_memory=False)
dataset=np.array(data)
print(dataset.shape)
np.random.shuffle(dataset)
x=dataset
y=dataset
x=x[:,1:2501]
y=y[:,0]
**#splitting the data into training and testing set, normalizing the values**
x_train=x[0:12000,:]    # 12000 samples in training set
x_train=x_train/255.     # coverting the pixel values0-255 into 0-1
x_test=x[12001:13345,:] # 1345 samples in testing set
x_test=x_test/255.
y=y.reshape(y.shape[0],1)
y_train=y[0:12000,:]
y_train=y_train.T
y_test=y[12001:13345,:]
y_test=y_test.T
print("no. of training examples:"+str(x_train.shape[0]))
print("no. of test examples:"+str(x_test.shape[0]))
print("x_train shape:"+ str(x_train.shape))
print("x_test shape:"+ str(x_test.shape))
print("y_train shape:"+str(y_train.shape))
print("y_test shape: "+str(y_test.shape))

**output:**
no. of training examples:6672
no. of test examples:0
x_train shape:(6672, 2500)
x_test shape:(0, 2500)
y_train shape:(1, 6672)
y_test shape: (1, 0)

【问题讨论】:

  • 我在我的系统上运行了上面的代码段,它运行良好。请检查您的案例中代码的其他部分是否未修改变量。
  • Ya 代码没有显示任何错误,它仅重新定位 csv 文件的初始 6675 行,其余行未读取
  • 在我的系统中,行数上升到 12000。尝试在您的系统上运行此代码段并让我们知道结果。我用随机数据创建了一个类似的数据框。 codeshare.io/2WwjAv
  • 谢谢,但我已经重新检查过,变量没有变化......我也更新了熊猫,但它仍然没有完全读取 csv 文件
  • 我什至试图分块阅读它,但仍然没有完全阅读。难道只有csv文件有问题吗

标签: python pandas numpy csv tensorflow


【解决方案1】:

您可以尝试按照文档中的说明按块读取 CSV:

reader = pd.read_csv('tmp.sv', sep='|', chunksize=4)
for chunk in reader:
    print(chunk)

【讨论】:

  • 我也用过这个,但它仍然只读取最初的 6675 行
猜你喜欢
  • 1970-01-01
  • 2020-03-02
  • 2016-12-12
  • 2015-02-17
  • 2021-08-12
  • 2022-08-11
  • 2016-10-02
  • 2017-11-30
  • 1970-01-01
相关资源
最近更新 更多