1

I'm fairly new to coding (and this type of website) and right now I'm having difficulties with python. I need python to read data from a CSV file:

Here's a snippet of my code:

import numpy as np
data = np.loadtext('Users/User/Documents/Data.csv', delimiter=',')
firstrow = data[0:,]

Here is just a sample of the CSV (the actual file is very large and contains a row of 2000+ numbers)

2   -2  2   5   -4  -2  0   4   -5

I want python to read the first row of the file but whenever I run the program it is always saying "could not convert string to float". I don't understand what the problem is here and how I can fix it without making a new file (as mentioned before the file is very very large and it would take me a very long time to remake) but any help would be very much appreciated, thanks!

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
user106039
  • 21
  • 1
  • 2
  • Shouldn't the delimiter be tab or `delimiter= None` for any whitespace – Akshay Hazari Dec 03 '15 at 06:04
  • Just to make sure: The first line of the csv file doesn't contain headers, does it? – Tim Pietzcker Dec 03 '15 at 06:09
  • Hey guys thanks for the quick response! When I set the delimiter to delimiter= None it still says "could not convert string to float" but this time with the list of numbers in the row terminal. And no there is no headers in the csv file. I can link the file if need be? – user106039 Dec 03 '15 at 06:13
  • Try using [np.genfromtxt](http://docs.scipy.org/doc/numpy/reference/generated/numpy.genfromtxt.html) instead – mauve Dec 03 '15 at 06:23
  • @user106039 Please let us know how we can further help resolve your question. Otherwise, if you find one of the answers suitable please accept it. – ilyas patanam Dec 09 '15 at 17:59

1 Answers1

0

I would use pandas. It is much faster than np.loadtext.

import pandas as pd
data = pd.read_csv('Users/User/Documents/Data.csv', header = None)

The default delimiter is a comma, however if you want to pass another delimiter such as tab just do data = pd.read_csv('Users/User/Documents/Data.csv', sep = '\t'). If the delimiter should be any whitespace instead do data = pd.read_csv('Users/User/Documents/Data.csv', delim_whitespace = True).

If you only need to read the first row of your data, then you can just do pd.read_csv('Users/User/Documents/Data.csv', nrows = 1)

To convert from a pandas dataframe to numpy array, just do:

data_np = data.values
Community
  • 1
  • 1
ilyas patanam
  • 5,116
  • 2
  • 29
  • 33