Answers.Body: <p>I am not able to reproduce the issue.</p>
  <p>However I think this is a <a href="https://pandas.pydata.org/docs/reference/api/pandas.crosstab.html" rel="nofollow noreferrer">crosstab</a> problem:</p>
  <pre><code>&gt;&gt;&gt; new_df = df.fillna(&quot;no&quot;)
  &gt;&gt;&gt; new_df
  # prints:
      Sample_code Group smoking disease_present
  0             1     A       y             yes
  1             2     B       n              no
  2             3     C       n             yes
  3             4     D       y              no
  4             5     B       n             yes
  5             6     A       y             yes
  6             7     C       n              no
  7             8     D       y              no
  8             9     A       n             yes
  9            10     B       y              no
  10           11     D       n             yes
  11           12     C       n              no
  12           13     A       y             yes
  13           14     B       y             yes
  
  &gt;&gt;&gt; pd.crosstab([new_df.disease_present, new_df.smoking], new_df.Group)
  # prints
  Group                    A  B  C  D
  disease_present smoking            
  no              n        0  1  2  0
                  y        0  1  0  2
  yes             n        1  1  1  1
                  y        3  1  0  0
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-03-06T18:27:45.270Z
Answers.LastActivityDate: 2023-03-06T18:27:45.270Z
Answers.UserDisplayName: Maximilian Press
Body: <p>I was trying to calculate the frequency of disease_present (<code>yes</code>) when smoking status is <code>y</code> (yes) for each group (A, B, C, D)</p>
  <p><code> The formula is = Number of samples with disease_present (yes) / No of samples with smoking status 'y' * 100</code></p>
  <p><strong>df</strong></p>
  <pre><code>Sample_code Group   smoking disease_present
  1   A   y   yes
  2   B   n   
  3   C   n   yes
  4   D   y   
  5   B   n   yes
  6   A   y   yes
  7   C   n   
  8   D   y   
  9   A   n   yes
  10  B   y   
  11  D   n   yes
  12  C   n   
  13  A   y   yes
  14  B   y   yes
  </code></pre>
  <p>I tried this</p>
  <pre><code>df.groupby(&quot;Group&quot;).count()
  </code></pre>
  <p>This gave the same number in every category</p>
  <ol>
  <li>Pandas by default turns empty cells into NaN</li>
  <li>I tried to replace NaN with empty strings with <code>df1 = df.fillna(&quot;&quot;)</code>. I still got the same results</li>
  </ol>
  
Tags: <python><pandas>
Title: How to calculate frequency of a category with respect to the value in another column?
ThreadText: # How to calculate frequency of a category with respect to the value in another column?
  
  (Asked by: username_0 on 2023-03-06)
  
  I was trying to calculate the frequency of disease\_present (`yes`) when smoking status is `y` (yes) for each group (A, B, C, D)
  
  `The formula is = Number of samples with disease_present (yes) / No of samples with smoking status 'y' * 100`
  
  **df**
  
  ```
  Sample_code Group   smoking disease_present
  1   A   y   yes
  2   B   n   
  3   C   n   yes
  4   D   y   
  5   B   n   yes
  6   A   y   yes
  7   C   n   
  8   D   y   
  9   A   n   yes
  10  B   y   
  11  D   n   yes
  12  C   n   
  13  A   y   yes
  14  B   y   yes
  
  ```
  
  I tried this
  
  ```
  df.groupby("Group").count()
  
  ```
  
  This gave the same number in every category
  
  1. Pandas by default turns empty cells into NaN
  2. I tried to replace NaN with empty strings with `df1 = df.fillna("")`. I still got the same results
  
  **Tags:** python, pandas
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-03-06. Score: 2 | Accepted answer
  
  I am not able to reproduce the issue.
  
  However I think this is a [crosstab](https://pandas.pydata.org/docs/reference/api/pandas.crosstab.html) problem:
  
  ```
  >>> new_df = df.fillna("no")
  >>> new_df
  # prints:
      Sample_code Group smoking disease_present
  0             1     A       y             yes
  1             2     B       n              no
  2             3     C       n             yes
  3             4     D       y              no
  4             5     B       n             yes
  5             6     A       y             yes
  6             7     C       n              no
  7             8     D       y              no
  8             9     A       n             yes
  9            10     B       y              no
  10           11     D       n             yes
  11           12     C       n              no
  12           13     A       y             yes
  13           14     B       y             yes
  
  >>> pd.crosstab([new_df.disease_present, new_df.smoking], new_df.Group)
  # prints
  Group                    A  B  C  D
  disease_present smoking            
  no              n        0  1  2  0
                  y        0  1  0  2
  yes             n        1  1  1  1
                  y        3  1  0  0
  
  ```
  
  
  ---
  
  