0

My h.csv file contains 7 columns of float data type and the delimiter is a white space

my program in python is as follows

import pandas as pd
import csv
import numpy as np 

h = np.array(pd.read_csv("h.csv", delim_whitespace=True))
print(h)
print("\n")
X = h[:,0:6]
print(X)
print("\n")
y = h[:,6]
print(y)
print("\n")

I am encountering the problem with the print statement that prints y;

Actually it has to print the last column of my file, but it is not doing so.

The arrays 'h' and 'X' are printing well. The print 'y' statement is not printing the last column of the matrix, but printing some random numbers.

hanugm
  • 1,127
  • 3
  • 13
  • 44
  • check the source file, it seems they may have space and the end. if you have less column, then following error will be shown `IndexError: index 6 is out of bounds for axis 1 with size 6` – Shijo Sep 07 '17 at 20:14
  • @Shijo It is not showing any wrror, but printing random numbers... – hanugm Sep 07 '17 at 20:15
  • please share the sample data – Shijo Sep 07 '17 at 20:16
  • @Shijo http://archive.ics.uci.edu/ml/machine-learning-databases/00243/yacht_hydrodynamics.data – hanugm Sep 07 '17 at 20:19
  • I dont see any issues with the code and the numbers are looking correct.But noticed that you dont have a header, so add parameter header=None to ' h = np.array(pd.read_csv("h.csv", delim_whitespace=True,header=None)) ' – Shijo Sep 07 '17 at 20:24
  • @Shijo I tried it too, but not getting last column...... – hanugm Sep 07 '17 at 20:27
  • paste your sample input data , expected output and current output here, not in any other site, we will try to help you – Shijo Sep 07 '17 at 20:28

1 Answers1

0

just add names to the columns and print by them, for example:

h = pd.read_csv("h.csv", delim_whitespace=True, names = ["A", "B", "C", "X","Y","Z","P"])

print (h.A)
print (h.B)
print (h.Y)
print (h.P)
Yuval Raz
  • 96
  • 7
  • first of all, you are getting the Scientific Notation of the numbers second of all you don't need numpy to add the csv to the array. pandas will do. you are not getting the first row when you are importing it like that. – Yuval Raz Sep 07 '17 at 20:29
  • How to suppress scientific notation? – hanugm Sep 07 '17 at 20:35
  • my edited answer should help you better now. – Yuval Raz Sep 07 '17 at 20:50