PRACTICAL DATA CLEANING Cleaning US Census Data

Hi,
I am having an issue applying the pandas groupby method on one of the exercises.
It produces an error ‘AttributeError: Cannot access callable attribute ‘reset_index’ of ‘SeriesGroupBy’ objects, try using the ‘apply’ method’. However I dont understand as my variable ‘us_census’ is a dataframe.
Below is my entire code

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import codecademylib3_seaborn
import glob

files = glob.glob("states*.csv")
df = []
for n in files:
  data = pd.read_csv(n)
  df.append(data)
us_census = pd.concat(df)

print(us_census.dtypes)
print(us_census.columns)

us_census.Income = us_census.Income.replace('\$','', regex=True)
us_census.Income = pd.to_numeric(us_census.Income)


df1 = us_census.GenderPop.str.split('_', expand=True)
print(df1.head())

us_census['Men'] = df1[0]
us_census['Women'] = df1[1]


us_census.Men = us_census.Men.replace('M','',regex=True)
us_census.Women = us_census.Women.replace('F','',regex=True)
us_census = us_census.drop(columns=['GenderPop'])


us_census.Men = pd.to_numeric(us_census.Men)
us_census.Women = pd.to_numeric(us_census.Women)
print(us_census.head())
print(us_census.dtypes)

df2 = us_census.groupby('State').Women.reset_index()
print(df2)

This is the link to this exercise
https://www.codecademy.com/paths/data-science/tracks/practical-data-cleaning/modules/data-cleaning-with-pandas/projects/data-cleaning-us-census

Thanks for helping :upside_down_face:

1 Like

I just realised that the groupby was not working becuase I wasnt actually performing an aggragrate function. My goal was just to see two columns side by side which is quite simple.

2 Likes