Help in Python - User-defined function to count occurrences of items


I would like to use python to write a user-defined function that has 2 parameters: a column index and a transport method. The data are from a csv file.

It counts the occurrences of the transport method in the column index of the NumPy array and returns the total occurrences. May i know how should i go about it?

Thank you very much in advance.

Well forget the function for a moment. How would you do those steps normally?
How to a) select a column index and b) count occurrences of a particular value in an array.

1 Like

Sorry may I know how could I link this code to get a user-defined function?

Are you familiar with functions? You already described the parameters so the only thing left would be the return.

As for the actual logical steps the only section missing from the process is converting your transport type into a numerical index. Something like a dictionary is great for this kind of mapping, e.g. {"Plane": 4} but you could get that index in other ways too.

1 Like