QBoard » Artificial Intelligence & ML » AI and ML - Python » Grouping By A Column For A Total Count in Python

Grouping By A Column For A Total Count in Python

  • I am working on a problem for a Intro to Data Science course on Coursera, and I am struggling with adding data to a column in a dataframe.

    This is the data set I'm working with:

    SUMLEV  REGION  DIVISION    STATE   COUNTY  STNAME  CTYNAME     
    1   50      3       6           1       1       Alabama Autauga County  
    2   50      3       6           1       3       Alabama Baldwin County  
    3   50      3       6           1       5       Alabama Barbour County  
    4   50      3       6           1       7       Alabama Bibb County 

     

    What I am trying to do is to insert a column called TotalCounties that has the total count of counties by state as a last column. I've done similar things in SQL, but it doesn't seem to work quite the same in Python.

    I have tried the code below, but the column ends up displaying as NaN instead of a number like I want it to.

    counties_only_df = census_df[census_df['SUMLEV'] == 50]
       x = counties_only_df.groupby('STNAME').count()['SUMLEV']
       counties_only_df['Total Counties'] = x

     

    I would like a number to display in the newly created column instead of NaN.

     
      January 10, 2022 12:42 PM IST
    0