SoFunction
Updated on 2024-11-13

Converting csv files to arrays and slicing arrays

In Python we often use two libraries Numpy and pandas

Converting csv files to arrays

import numpy
my_matrix = (open("c:\\","rb"),delimiter=",",skiprows=0) //CSVConverting files to arrays

Storing an array or matrix as a csv file can be achieved using the following code:

('', my_matrix, delimiter = ',')

Slicing of arrays

An array slice is a view of the original array, meaning that the data is not copied and any modifications to the view are directly reflected in the original array:

1D array slice

>>> arr2=(10)>>> arr2array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])>>> arr2[5:8]array([5, 6, 7])>>> arr[5:8]=12>>> arr2array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])>>> arr_slice=arr2[5:8]>>> arr_slice[1]=12345>>> arr2array([  0,   1,   2,   3,   4,  12, 12345,  12,   8,   9])>>> arr_slice[:]=64>>> arr2array([ 0, 1, 2, 3, 4, 64, 64, 64, 8, 9])

2D array slice

2D slicing is axis-dependent and can be performed on one or more axes

>>> import numpy as np
>>> arr = (12).reshape((3, 4))
>>> print(arr)
[[ 0 1 2 3]
 [ 4 5 6 7]
 [ 8 9 10 11]]
>>> slice_one = arr[1:2, 1:3]
>>> print(slice_one)
[[5 6]]
>>> arr[:2]
array([[0, 1, 2, 3],
    [4, 5, 6, 7]])
>>> arr[:2,1:]
array([[1, 2, 3],
    [5, 6, 7]])

The above article to convert csv files into arrays and arrays of slicing methods is all that I have shared with you.