Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas Group Columns by Value of 1 and Sort By Frequency

I have to take this dataframe:

d = {'Apple': [0,0,1,0,1,0], 'Aurora': [0,0,0,0,0,1], 'Barn': [0,1,1,0,0,0]}
df = pd.DataFrame(data=d)

  Apple Aurora Barn
0   0   0   0
1   0   0   1
2   1   0   1
3   0   0   0
4   1   0   0
5   0   1   0

And count the frequency of the number one in each column, and create a new dataframe that looks like this:

df = pd.DataFrame([['Apple',0.3333], ['Aurora',0.166666], ['Barn', 0.3333]], columns = ['index', 'value'])

    index   value
0   Apple   0.333300
1   Aurora  0.166666
2   Barn    0.333300

I have tried this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df['freq'] = df.groupby(1)[1].transform('count')

But I get an error: KeyError: 1

So I’m not sure how to count the value 1 across rows and columns, and group by column names and the frequency of 1 in each column.

>Solution :

If I understand correctly, you could do simply this:

freq = df.mean()

Output:

>>> freq
Apple     0.333333
Aurora    0.166667
Barn      0.333333
dtype: float64
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading