I have created a numpy arraylist from a CSV file that has a couple of thousand elements.
First, I used the np.array function to convert a dataframe column into an array.
Then I used a function that calculates the most common element in a list, but it gave me an error.
The code I wrote is shown below.
col_array = np.array(dataframe.col)
def most_common(lst): return max(set(lst), key=lst.count)
CodePudding user response:
For your most_common
function, you need to cast the input to a list
:
import numpy as np
def most_common(lst):
lst = list(lst)
return max(set(lst), key=lst.count)
col_array = np.array([0,0,1,1,1,3])
print(most_common(col_array))
There are many other solutions, see e.g.; Find the most frequent number in a NumPy array
CodePudding user response:
np.argmax(np.bincount(<np.array>))
just use np.bincount if your data values are not very large