Import csv file into a matrix/Array in Python

后端 未结 3 1906
萌比男神i
萌比男神i 2021-01-18 07:20

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

相关标签:
3条回答
  • 2021-01-18 07:35

    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.

    0 讨论(0)
  • 2021-01-18 07:36

    You can use the built-in csv module to load your data to a multidimensional list:

    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]]
    
    0 讨论(0)
  • 2021-01-18 07:47
    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:])
    
    
    
    0 讨论(0)
提交回复
热议问题