Home » Python » python – How to read csv file to numpy ndarrays-Exceptionshub

python – How to read csv file to numpy ndarrays-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I am looking for a way to read my csv-file into an ndarrays.

myfile.csv

user,latitude,longitude
500,39.984608,116.317761
500,39.984563,116.317517
500,39.984539,116.317294
605,26.16167,119.943128
605,26.161566,119.942352
605,26.161558,119.942401
745,22.814336,108.332281
745,22.81429,108.3322566
745,22.81432,108.3322583

my code:

import numpy as np
my_data = np.genfromtxt('myfile.csv', delimiter=',', skip_header=True)

type(my_data)
numpy.ndarray

print(my_data)
[[500.         39.984608  116.317761 ]
 [500.         39.984563  116.317517 ]
 [500.         39.984539  116.317294 ]
 [605.         26.16167   119.943128 ]
 [605.         26.161566  119.942352 ]
 [605.         26.161558  119.942401 ]
 [745.         22.814336  108.332281 ]
 [745.         22.81429   108.3322566]
 [745.         22.81432   108.3322583]]

However, my intented output is to get an arrays of array, each array for one user, so that the output is:

[
  [[500.         39.984608  116.317761 ]
   [500.         39.984563  116.317517 ]
   [500.         39.984539  116.317294 ]]
  [[605.         26.16167   119.943128 ]
   [605.         26.161566  119.942352 ]
   [605.         26.161558  119.942401 ]]
  [[745.         22.814336  108.332281 ]
   [745.         22.81429   108.3322566]
   [745.         22.81432   108.3322583]]
]

How do I rewrite my code to do this?

How to&Answers:

This solution will give you a numpy.ndarray partitioned by the first column of my_data. If order matters, you can sort either partition_values in the comprehension, or sort grouped_values.

import numpy as np


my_data = np.genfromtxt('myfile.csv', delimiter=',', skip_header=True)
partition_values = {row[0] for row in my_data}
grouped_data = np.array([my_data[my_data[:,0] == pvalue, :]
                         for pvalue in partition_values])

Answer:

Try this:

def getArraysofArray(my_data):
    FinalList=[]
    temp=[]
    for i in range(len(my_data)):
        if(i==0):
            temp.append(my_data[i])
            continue
        if(my_data[i][0]!=my_data[i-1][0]):
            FinalList.append(temp)
            temp=[]
        if(my_data[i]==my_data[-1]):
            FinalList.append(temp)
        temp.append(my_data[i])
    return FinalList

# Main / Testing of Function
my_data=[[500,39.984608 ,116.317761]
 ,[500,39.984563,116.317517]
 ,[500,39.984539,116.317294]
 ,[605,26.16167,119.943128]
 ,[605,26.161566,119.942352]
 ,[605,26.161558,119.942401]
 ,[745,22.814336,108.332281]
 ,[745,22.81429,108.3322566]
 ,[745,22.81432,108.3322583]]
list=getArraysofArray(my_data)
print(list)
# Output
[[[500, 39.984608, 116.317761], [500, 39.984563, 116.317517], [500, 39.984539, 116.317294]],
 [[605, 26.16167, 119.943128], [605, 26.161566, 119.942352], [605, 26.161558, 119.942401]],
 [[745, 22.814336, 108.332281], [745, 22.81429, 108.3322566], [745, 22.81432, 108.3322583]]]