The Python Oracle

Pandas Percentage count on a DataFrame groupby

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Techno Bleepage Open

--

Chapters
00:00 Pandas Percentage Count On A Dataframe Groupby
00:51 Accepted Answer Score 16
01:05 Answer 2 Score 1
01:40 Answer 3 Score 0
01:53 Answer 4 Score 0
02:21 Thank you

--

Full question
https://stackoverflow.com/questions/3212...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pandas

#avk47



ACCEPTED ANSWER

Score 16


Could be just this:

In [73]:

print pd.DataFrame({'Percentage': df.groupby(('ID', 'Feature')).size() / len(df)})
            Percentage
ID Feature            
0  False           0.2
   True            0.3
1  False           0.3
   True            0.2



ANSWER 2

Score 1


In [2]: df = pd.DataFrame({'Index': {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 7, 7: 8, 8: 9, 9: 10},
   ...:  'Feature': {0: True, 1: True, 2: False, 3: True, 4: False, 5: False, 6: True, 7: False, 8: False, 9: True},
   ...:  'ID': {0: 1, 1: 1, 2: 0, 3: 0, 4: 1, 5: 1, 6: 0, 7: 0, 8: 1, 9: 0},
   ...:  'Stuff1': {0: 23, 1: 54, 2: 45, 3: 38, 4: 32, 5: 59, 6: 37, 7: 76, 8: 32, 9: 23},
   ...:  'Stuff2': {0: 12, 1: 12, 2: 67, 3: 29, 4: 24, 5: 39, 6: 32, 7: 65, 8: 12, 9: 15}}).sort_values(["ID", "Feature"])
   ...: df
Out[2]: 
   Index  Feature  ID  Stuff1  Stuff2
2      3    False   0      45      67
7      8    False   0      76      65
3      4     True   0      38      29
6      7     True   0      37      32
9     10     True   0      23      15
4      5    False   1      32      24
5      6    False   1      59      39
8      9    False   1      32      12
0      1     True   1      23      12
1      2     True   1      54      12

In [3]: f = df.drop_duplicates(subset=['Feature', 'ID'])
   ...: f2 = (df.groupby(["Feature", "ID"]).agg('count')/len(df)*100).iloc[:, 0].reset_index().rename(columns={"Index" : "Percent"})
   ...: f2['Percent'] = f2['Percent'].astype(int).astype(str) + "%"
   ...: f2
Out[3]: 
   Feature  ID Percent
0    False   0     20%
1    False   1     30%
2     True   0     30%
3     True   1     20%



ANSWER 3

Score 0


You can use pd.crosstab:

>>> newdf = pd.crosstab(index=mydf['Feature'], columns=mydf['ID']).stack()/len(mydf)
>>> print(newdf)
Feature  ID
False    0     0.2
         1     0.3
True     0     0.3
         1     0.2
dtype: float64



ANSWER 4

Score 0


You could also use the tableone package for this. Create the sample dataframe:

# Create df with 10 rows.
df = pd.DataFrame({'Feature': [True,True,False,True,False,False,True,False,False,True], 
    'ID': [1,1,0,0,1,1,0,0,1,0],
    'Stuff1': [23,54,45,38,32,59,37,76,32,23],
    'Stuff2': [12,12,67,29,24,39,32,65,12,15]})

Input:

enter image description here

# Import the tableone package (v0.5.18)
from tableone import TableOne

# Create the table, specifying feature and id as categorical
TableOne(df, columns=['Feature','ID'], 
    categorical=['Feature','ID'],
    label_suffix=True)

Output:

enter image description here