Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas Dataframe display total

Here is an example dataset found from google search close to my datasets in my environment

enter image description here

I’m trying to get output like this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

import pandas as pd
import numpy as np

data = {'Product':['Box','Bottles','Pen','Markers','Bottles','Pen','Markers','Bottles','Box','Markers','Markers','Pen'],
        'State':['Alaska','California','Texas','North Carolina','California','Texas','Alaska','Texas','North Carolina','Alaska','California','Texas'],
        'Sales':[14,24,31,12,13,7,9,31,18,16,18,14]}

df=pd.DataFrame(data, columns=['Product','State','Sales'])
df1=df.sort_values('State')
#df1['Total']=df1.groupby('State').count()   
df1['line']=df1.groupby('State').cumcount()+1
print(df1,index=False)

 
 Commented out line throws this error 
 ValueError: Columns must be same length as key
 
Tried with size() it gives NaN for all rows

Hope someone points me to right direction
Thanks in advance

>Solution :

I think this should work for ‘Total’:

df1['Total']=df1.groupby('State')['Product'].transform(lambda x: x.count())  
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading