Search code examples
pythonpandaspivot-table

Pivot table not working due to multi-dimensionality


I have a dataframe where I have data such as domicile, age, gender, etc. I want to create a pivot table where it shows the state-wise gender distribution of the respondents. So I wrote the following line of code

table = pd.pivot_table(sample_df, values = ["gender"], index = ["domicile_state"], columns = ["gender"], aggfunc = np.count)

However, this shows an error that numpy doesn't have any function called count. If I use np.sum instead of np.count, it just adds all the male/female responses end to end, something like MaleMaleMale.... So it's obviously doing string operations. However, if I remove the aggfunc, I get an error Grouper for 'gender' not 1-dimensional So basically I want a table like

---------------------------
domicile | gender | count |
---------------------------
A         Male      12
          female    14
B         Male      16
          Female    10
--------------------------- 

I don't have any other column with that name, So I don't understand what is the issue.


Solution

  • np.count -> 'count'

    pd.pivot_table(sample_df, values=["gender"], index=["domicile_state"], columns=["gender"], aggfunc='count')