genfromtxt

Reading csv with numpy has extra row of 1's

Reading csv with numpy has extra row of 1's Question: I am using np.genfromtxt to read a csv file, and trying to use the converters argument to preprocess each column. CSV: "","Col1","Col2","Col3" "1","Cell.1",NA,1 "2","Cell.2",NA,NA "3","Cell.3",1,NA "4","Cell.4",NA,NA "5","Cell.5",NA,NA "6","Cell.6",1,NA Code: import numpy as np filename = ‘b.csv’ h = ("", "Col1", "Col2", "Col3") def col1_converter(v): print(f’col1_converter …

Total answers: 1

Use np.genfromtxt to read data of different dtypes in csv file

Use np.genfromtxt to read data of different dtypes in csv file Question: I am trying to read a csv file of that looks like: label,value first,1.234e-01 second,5.678e-02 three,9.876e-03 … etc Where the first column contains strings and the second column contains floats. From the online documentation of np.genfromtxt I thought that the line file_data = …

Total answers: 1

Add a space in 2D array when writing a text file

Add a space in 2D array when writing a text file Question: I am trying to store a 2D vector into a .DAT file and I would like to add a space at the start of every row. An example of a desired output looks like this: 0.0000000E+00 0.0000000E+00 2.0020020E-03 0.0000000E+00 4.0040040E-03 0.0000000E+00 6.0060060E-03 0.0000000E+00 …

Total answers: 2

Read in all csv files from a directory using Python

Read in all csv files from a directory using Python Question: I hope this is not trivial but I am wondering the following: If I have a specific folder with n csv files, how could I iteratively read all of them, one at a time, and perform some calculations on their values? For a single …

Total answers: 9

Using numpy.genfromtxt to read a csv file with strings containing commas

Using numpy.genfromtxt to read a csv file with strings containing commas Question: I am trying to read in a csv file with numpy.genfromtxt but some of the fields are strings which contain commas. The strings are in quotes, but numpy is not recognizing the quotes as defining a single string. For example, with the data …

Total answers: 5

numpy.genfromtxt produces array of what looks like tuples, not a 2D array—why?

numpy.genfromtxt produces array of what looks like tuples, not a 2D array—why? Question: I’m running genfromtxt like below: date_conv = lambda x: str(x).replace(“:”, “/”) time_conv = lambda x: str(x) a = np.genfromtxt(input.txt, delimiter=’,’, skip_header=4, usecols=[0, 1] + radii_indices, converters={0: date_conv, 1: time_conv}) Where input.txt is from this gist. When I look at the results, it …

Total answers: 1

How do I read CSV data into a record array in NumPy?

How do I read CSV data into a record array in NumPy? Question: Is there a direct way to import the contents of a CSV file into a record array, just like how R’s read.table(), read.delim(), and read.csv() import data into R dataframes? Or should I use csv.reader() and then apply numpy.core.records.fromrecords()? Asked By: hatmatrix …

Total answers: 14