Numpy: genfromtxt forming tuples

numpy genfromtxt example
numpy dtype
numpy structured array
numpy genfromtxt converters
numpy genfromtxt vs loadtxt
numpy genfromtxt multiple delimiters
numpy recfunctions
numpy genfromtxt datetime64

Here is my menu.csv:

Item,Price
Curry Rice,3.5
Pork Chop,6
Seafood Soup,5
Salad,2.8

Here is my code:

import numpy as np
menu_items = np.genfromtxt("menu.csv", delimiter=',',names=True)
print(menu_items)

What I get:

[(nan, 3.5) (nan, 6.2) (nan, 3. ) (nan, 2.8)]

When I use dtype=None:

[(b'Curry Rice', 3.5) (b'Pork Chop', 6.2) (b'Seafood Soup', 3. )
 (b'Salad', 2.8)]

What I want:

[(Curry Rice, 3.5) (Pork Chop, 6.2) (Seafood Soup, 3. ) (Salad, 2.8)]

Any help is appreciated

By default numpy.genfromtxt() assumes that the data-type of each column is a float. You can ask it to try and guess the data-type of each column by passing it the keyword argument dtype=None.

menu_items = np.genfromtxt("menu.csv", delimiter=',', names=True, dtype=None)

Importing data with genfromtxt — NumPy v1.20.dev0 Manual, The only mandatory argument of genfromtxt is the source of the data. It can be a string, a sequence of tuples (name, type) , such as dtype=[('A', int), ('B', float)] . If your data file is structured like this. col1, col2, col3 1, 2, 3 10, 20, 30 100, 200, 300 then numpy.genfromtxt can interpret the first line as column headers using the names=True option.

Welcome!

I think your question looks very similar to How to use numpy.genfromtxt when first column is string and the remaining columns are numbers? . And it looks extensively answered. Have a look there and also check the dtype option for np.genfromtxt in the python doc

Structured arrays — NumPy v1.19 Manual, Each tuple has the form (fieldname, datatype, shape) where shape is optional. fieldname is a string (or tuple if titles are used, see Field Titles� Python versions. As of Janurary 1, 2020, Python has officially dropped support for python2. For this class all code will use Python 3.7.Ensure you have gone through the setup instructions and correctly installed a python3 virtual environment before proceeding with this tutorial.

With your sample file:

In [349]: cat stack58789967.txt                                                 
Item,Price
Curry Rice,3.5
Pork Chop,6
Seafood Soup,5
Salad,2.8

In [350]: np.genfromtxt('stack58789967.txt',delimiter=',',names=True, dtype=None)                                                                     
/usr/local/bin/ipython3:1: VisibleDeprecationWarning: Reading unicode 
   strings without specifying the encoding argument is deprecated. Set the 
   encoding, use None for the system default.
  #!/usr/bin/python3
Out[350]: 
array([(b'Curry Rice', 3.5), (b'Pork Chop', 6. ), (b'Seafood Soup', 5. ),
       (b'Salad', 2.8)], dtype=[('Item', 'S12'), ('Price', '<f8')])

In [351]: np.genfromtxt('stack58789967.txt',delimiter=',',names=True, dtype=None, encoding=None)                                                      
Out[351]: 
array([('Curry Rice', 3.5), ('Pork Chop', 6. ), ('Seafood Soup', 5. ),
       ('Salad', 2.8)], dtype=[('Item', '<U12'), ('Price', '<f8')])

'S12' is bytestring dtype, one byte per character. This is the Py2 norm. 'U12' is unicode dtype, 4 bytes per character. This is the Py3 norm.

The 'tuples' here mark the records of a structured array.

The array is 1d, and fields are accessed by name:

In [352]: _.shape                                                               
Out[352]: (4,)
In [353]: __['Item']                                                            
Out[353]: array(['Curry Rice', 'Pork Chop', 'Seafood Soup', 'Salad'], dtype='<U12')

Numpy array of tuples, Oct 07, 2016 � Learn to create NumPy arrays from lists or tuples in this video as a numpy array. array), an n-dimensional array that is also present in some form in I'm running genfromtxt like below: python import numpy genfromtxt. ndarray. Chapter 4. NumPy Basics: Arrays and Vectorized Computation NumPy, short for Numerical Python, is the fundamental package required for high performance scientific computing and data analysis. It is the foundation … - Selection from Python for Data Analysis [Book]

Read file as a list of tuples, python create tuple from text file python read text file into list of tuples One of the most efficient way to read delimited data like this is using numpy.genfromtxt . rand (1, 2, 3) * 100 np_print (numpy_array) The output with the default settings looks like this: Apr 21, 2020 · However, in such a case, the function will return a NumPy array of tuples of values since a NumPy array as a whole can have only 1 data type. tofile¶ method. … The result is a two-dimensional array. 1.

Can anyone help with converting a text file to a 2-D array in Python , I managed to reconstruct the image, i.e. form a 30x60x3 matrix. http:// stackoverflow.com/questions/12319969/how-to-use-numpy-genfromtxt-when-first -column-is- @Tyler Davis, actually, I have the tuples in a .txt file, not the actual image. 3. imag : ndarray Imaginary part of the array. arange(5) # a is printed. rand (1, 2, 3) * 100 np_print (numpy_array) The output with the default settings looks like this: Apr 21, 2020 · However, in such a case, the function will return a NumPy array of tuples of values since a NumPy array as a whole can have only 1 data type.

Read file as a list of tuples, Or: return tuple(f(x) Python- Creating a tuple from a txt file - Stack Overflow. I tried with numpy (using genfromtxt) but it didn' I am new to python and I have join them by spaces to form the string, and write the string with a new line to the file. numpy.array¶ numpy.array (object, dtype=None, *, copy=True, order='K', subok=False, ndmin=0) ¶ Create an array. Parameters object array_like. An array, any object exposing the array interface, an object whose __array__ method returns an array, or any (nested) sequence. dtype data-type, optional. The desired data-type for the array.

Comments
  • Possible duplicate of How to use numpy.genfromtxt when first column is string and the remaining columns are numbers?
  • The 'b' indicates bytestring values, a S dtype. In Py3 unicode is the standard string type. Try adding encoding=None to your genfromtxt call
  • Hi, when I use dtype=None, I have b' in front of the menu items (I edited the page for clarification)