Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
350 views
in Technique[技术] by (71.8m points)

python - Pandas groupby to find percent True and False

I have a column of sites: ['Canada', 'USA', 'China' ....]

Each site occurs many times in the SITE column and next to each instance is a true or false value.

INDEX | VALUE | SITE

0     | True  | Canada
1     | False | Canada
2     | True  | USA
3     | True  | USA

And it goes on.

Goal 1: I want to find, for each site, what percent of the VALUE column is True.

Goal 2: I want to return a list of sites where % True in the VALUE column is greater than 10%.

How do I use groupby to achieve this? I only know how to use groupby to find the mean for each site which won't help me here.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Something like this:

In [13]: g = df.groupby('SITE')['VALUE'].mean()
In [14]: g[g > 0.1]
Out[14]: 
SITE
Canada    0.5
USA       1.0

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...