I found a problem in completing this python pandas exercise. I am asked to write a pandas program to display most frequent value in a random series and replace everything else as 'Other' in the series.
I can't do it in a series but kind of OK in a dataframe. The code is shown below. My problem is: I can only select the value from the first index of the frequency counts list (i.e. index[0]
), but it implies there is only one mode. What if there are more than one mode?
Grateful with your help. Thank you!
data19 = np.random.randint(46,50,size = 10)
df19 = pd.DataFrame(data19, columns = ["integer"])
print(df19)
freq19 = df19["integer"].value_counts()
print(freq19)
find_mode = df19["integer"] == freq19.index[0] #What if there are more than one mode?
df19.loc[~find_mode, "integer"] = "Other"
print(df19)