Convert from CSV to array in Python
You should use the csv
module:
import csv
results = []
with open("input.csv") as csvfile:
reader = csv.reader(csvfile, quoting=csv.QUOTE_NONNUMERIC) # change contents to floats
for row in reader: # each row is a list
results.append(row)
This gives:
[[0.000264, 0.000352, 8.7e-05, 0.000549],
[0.00016, 0.000223, 1.1e-05, 0.000142],
[0.008853, 0.006519, 0.002043, 0.009819],
[0.002076, 0.001686, 0.000959, 0.003107],
[0.000599, 0.000133, 0.000113, 0.000466],
[0.002264, 0.001927, 0.00079, 0.003815],
[0.002761, 0.00288, 0.001261, 0.006851],
[0.000723, 0.000617, 0.000794, 0.002189]]
Python: how to turn csv data in to array
Read the file and get the items as list from it:
import csv
results = []
with open('some_array.csv','r') as f:
lines = csv.reader(f)
for line in lines:
results.append([[int(i)] for i in line])
>>results
[[['11'], ['10'], ['8'], ['12'], ['13'], ['11']],
[['0'], ['1'], ['0'], ['2'], ['3'], ['0']],
[['5'], ['15'], ['13'], ['11'], ['18'], ['18']]]
Convert CSV to Array - Python
Maybe this is how it should be?
myList = []
import csv
with open(csvFileName) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
for row in csv_reader:
myList.append(row)
csv_file.close()
print(myList[0])
print(myList[0][1])
Converting CSV into Array in Python
You can do this:
with open("Solutions10.csv") as csvfile:
result = [eval(k) for k in csvfile.readlines()]
Edit: Karl is cranky and wants you todo this:
with open("Solutions10.csv") as csvfile:
result = []
for line in csvfile.readlines():
line = line.replace("[","").replace("]","")
result.append([int(k) for k in line.split(",")]
But you're the programmer so you can do what you want. If you trust your input file eval is fine.
How to convert columns from a csv file into arrays in python with the first value being the array variable name?
If you have a large number of samples in csv file and don't want to construct DataFrame
object, you can use csv
module and constract your lists in an iterable way without loading whole data in memory:
import csv
csv_file = 'sample.csv'
names = []
description = []
price = []
with open(csv_file, 'r') as f:
reader = csv.DictReader(f)
for row in reader:
names.append(row.get('name'))
description.append(row.get('description'))
price.append(row.get('price'))
print(names)
## ['Apples', 'White Bread', 'Wholemeal Bread']
print(description)
## ['A bag of 3 apples', 'A loaf of white bread', 'A loag of wholemeal bread']
print(price)
## ['1.75', '1.90', '1.45']
How to import a csv-file into a data array?
Assuming the CSV file is delimited with commas, the simplest way using the csv
module in Python 3 would probably be:
import csv
with open('testfile.csv', newline='') as csvfile:
data = list(csv.reader(csvfile))
print(data)
You can specify other delimiters, such as tab characters, by specifying them when creating the csv.reader
:
data = list(csv.reader(csvfile, delimiter='\t'))
For Python 2, use open('testfile.csv', 'rb')
to open the file.
I want to convert .csv file to a Numpy array
For this, you first create a list of CSV
files (file_names) that you want to append. Then you can export this into a single CSV
file by reshaping Numpy-Array. This will help you to move forward:
import pandas as pd
import numpy as np
combined_csv_files = pd.concat( [ pd.read_csv(f) for f in file_names ])
Now, if you want to Export these files into Single .csv-File, use like:
combined_csv_files.to_csv( "combined_csv.csv", index=False)
Now, in order to obtain Numpy Array, you can move forward like this:
data_set = pd.read_csv('combined_csv.csv', header=None)
data_frames = pd.DataFrame(data_set)
required_array = np.array(data_frames.values)
print(required_array)
Here you can also reshape Numpy Array by using:
required_array.shape = (100, 14, 79)
I have perform simple test on cmd to confirm this:
>>> y = np.zeros((2, 3, 4))
>>> y.shape
(2, 3, 4)
>>> y.shape = (3, 8)
>>> y
array([[ 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0.]])
How to read strings from a csv file and convert it into a integer array?
Using a dict
for a lookup and list comprehension
Ex:
check = {'low': 1, "high": 3}
with open('sample.csv') as infile:
csv_file = csv.reader(infile)
next(csv_file) # skip header
result = [[check[c] for c in row] for row in csv_file]
How do I read CSV data into a record array in NumPy?
Use numpy.genfromtxt()
by setting the delimiter
kwarg to a comma:
from numpy import genfromtxt
my_data = genfromtxt('my_file.csv', delimiter=',')
Related Topics
How to Count the Total Number of Words in a Pandas Dataframe Cell and Add Those to a New Column
How to Check Whether a Number Is Divisible by Another Number
How to Find a Minimum Value in a 2D Array Without Using Numpy or Flattened in Python
How to Add Parenthesis Around a Substring in a String
How to Retrieve SQL Result Column Value Using Column Name in Python
How to Save\Load Models in Spark\Pyspark
How to Convert a Django Queryset into List of Dicts
Broadcast One Channel in Numpy Array into Three Channels
Remove Partial String from Dataframe With Pandas
How to Convert Python Code to Application
How to Calculate Average a Dictionary from List of Dictionary Data
Identifying the Range of a Color in Hsv Using Opencv
Possible to Loop Through Excel Files With Differently Named Sheets, and Import into a List
Find Value in Dictionary Using Regex in Python