I am trying to import big csv files which contain both string and numeric matrix of data into Arrays/matrices in Python. In MATLAB I used to load the file and simply assign it to a matrix but it seems to be a bit tricky in Python. Could somebody advise please? I am quite new in Python. Thanks all.
3 Answers
You can use the built-in csv module to load your data to a multidimensional list in Python 2:
import csv
with open('data.csv', 'rb') as f:
reader = csv.reader(f)
data_as_list = list(reader)
print data_as_list
# [['data1', 1],
# ['data2', 2],
# ['data3', 3]]
And with Python 3:
import csv
with open('data.csv', 'rt') as f:
reader = csv.reader(f)
data_as_list = list(reader)
print(data_as_list)
# [['data1', 1],
# ['data2', 2],
# ['data3', 3]]
Comments
You can use pandas.
import pandas as pd
df = pd.from_csv('filename.csv')
If the delimiter is not ',' you can change the default value by using the sep keyword, for example:
df = pd.from_csv('filename.csv', sep='\')
You will get a dataframe that comes with powerful analysis functions.
2 Comments
DeepSpace
@user3376020 It's a library that you need to install, I'm sorry if it wasn't clear in my answer. Try `pip install pandas' from the command line, or visit pypi.python.org/pypi/pandas/0.16.0/#downloads
import numpy as np
with open("your_file.csv",'r')as f :
data_list = list(csv.reader(f, delimiter=";"))
data_array=np.array(data_list[1:])
2 Comments
marsnebulasoup
Put triple ``` above and below your code to format it
L. F.
Please include some explanation along with your code.