Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - valuecounts() method - display all results [duplicate]

I am a complete novice when it comes to Python so this might be badly explained.

I have a pandas dataframe with 2485 entries for years from 1960-2020. I want to know how many entries there are for each year, which I can easily get with the .value_counts() method. My issue is that when I print this, the output only shows me the top 5 and bottom 5 entries, rather than the number for every year. Is there a way to display all the value counts for all the years in the DataFrame?

like image 262
xceej Avatar asked Dec 29 '25 00:12

xceej


2 Answers

Use pd.set_options and set display.max_rows to None:

pd.set_option("display.max_rows", None)

Now you can display all rows of your dataframe.

  • Options and settings
  • pandas.set_option
like image 122
Corralien Avatar answered Dec 30 '25 13:12

Corralien


If suppose the name of dataframe is 'df' then use

counts = df.year.value_counts()
counts.to_csv('name.csv',index=False)

As the terminal can't display entire columns they just display the top and bottom by collapsing the remaining values, so try saving in a csv and see the records.

like image 32
venkatesh Avatar answered Dec 30 '25 14:12

venkatesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!