0

I am trying to plot the 10 most frequent values of a column named 'content' in my dataframe.

I think i'm not too far but my code needs to be improved.

Here it is : df_sample['content'].value_counts()[:10].plot().hist() but i have some troubles with the axes I want the content numbers on my X-axis and the frequencies on the Y-Axis.

I also tried this : a = df_sample['content'].value_counts()[:10]

a.plot(kind='hist') but it outputs the frequencies in the Y-Axis and none of my codes show the content numbers.

Thank you in advance for your help!

1 Answer 1

1

Try turning the results of value_counts into a frame with reset_index:

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt

np.random.seed(5)

df_sample = pd.DataFrame({'task_container_id': np.random.randint(1, 15, 100)})
plot_df = (
    df_sample['task_container_id'].value_counts()
        .head(10)
        .rename_axis('value')
        .reset_index(name='counts')
)

plot_df:

   value  counts
0      1      13
1     12      11
2      8       9
3     10       9
4      7       7
5     14       7
6      2       6
7      3       6
8      4       6
9      5       6

Then plot:

ax = plot_df.plot(x='counts', kind='hist')

plot

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! This was helpful. Just a question, for the Y-axis how do i write the counts values ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.