Pandas count and percentage by value for a column

Опубликовано: 03 Октябрь 2024
на канале: Softhints - Python, Linux, Pandas
15,208
155

Pandas count and percentage by value for a column

https://blog.softhints.com/pandas-cou...

Notebook:

https://github.com/softhints/python/b...

* read remote data from pdf
* calculate count and percent
* format percent in better output
* value_counts
* value_counts and percentage
* combine count and percentage

s = df.energy
counts = s.value_counts()
percent = s.value_counts(normalize=True)
percent100 = s.value_counts(normalize=True).mul(100).round(1).astype(str) + '%'
pd.DataFrame({'counts': counts, 'per': percent, 'per100': percent100})

Bonus tips:

pandas column renaming

df.columns = ['food', 'Portion size ', 'per 100 grams', 'energy']

---------------------------------------------------------------------------------------------------------------------------------------------------------------

Inventor Of GANs Joins Apple

https://www.i-programmer.info/news/10...



---------------------------------------------------------------------------------------------------------------------------------------------------------------
Code store

github: https://github.com/softhints/python/
bitbucket: https://bitbucket.org/softhints/

Socials

site: https://softhints.com/
Facebook:   / 435421910242028  
Facebook:   / softhints  
Twitter:   / softwarehints  
Discord:   / discord