Search code examples
pythondictionarypandasseries

Access the index of a pandas series


I am trying to identify which word is the most counted in a pandas dataframe (df_temp in my code). Also I have this :

 l = df_temp['word'].count_values()

l is then obviously a pandas series where the first row points toward the most counted index (in my case the most counted word) in df_temp['word']. Although I can see the word in my console, I cannot get it properly. The only way I found so far is to transform it into a dictionary so I have :

dl = dict(l)

and then I can easily retrieve my index...after sorting the dictionary. Obviously this does the job, but I am pretty sure you have a smarter solution as this one is very dirty and inelegant.


Solution

  • Using Pandas you can find the most frequent value in the word column:

    df['word'].value_counts().idxmax()
    

    and this code below will give you the count for that value, which is the max count in that column:

    df['word'].value_counts().max()