0

I have this dataframe shown in the picture below: enter image description here

There are more categories shown and my goal is to slice through each of the 33 categories to get the number of unique usernames. I though about doing something like this:

SexualHealth = df2.loc["Sexual Health & STDs"]
WomenHealth = df2.loc["Women's Health"]
WeightLoss = df2.loc["Weight Loss"]
MentalHealth = df2.loc["MentalHealth"]

Then go to each dataframe and do this:

SexualHealth_unique= SexualHealth['username'].value_counts()

But I would like to know if there is a faster way to get the number of unique users for each category in a single command of something that is fastener that what I am doing right now.

Data1234
  • 87
  • 8
  • IIUC you want `df2.groupby('category')['category'].size()` – EdChum Feb 07 '17 at 15:32
  • I tried it and did not work. I have already grouped them by category so when I open the dataframe all the posts from SexualHealth for example will be in order followed by all the posts in WomenHealth. Now I just want to find the number of unique user in each one. – Data1234 Feb 07 '17 at 15:37
  • So are the categories now the index now? so you could do `df2.groupby(level=0).size()`? – EdChum Feb 07 '17 at 15:37
  • I have done that too. My problem is since I get that how to find the unique username in each one – Data1234 Feb 07 '17 at 15:40
  • so you want something like `df2.groupby(level=0).apply(lambda x: x.value_counts())` if this doesn't work you need to post raw data, your code and the desired result to help us help you – EdChum Feb 07 '17 at 15:41
  • Refrain from showing your dataframe as an image. Your question needs a minimal reproducible example consisting of sample input, expected output, actual output, and only the relevant code necessary to reproduce the problem. See [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) for best practices related to Pandas questions. – itprorh66 Feb 03 '23 at 15:40

0 Answers0