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
184 views
in Technique[技术] by (71.8m points)

python - Efficiently grouping the rows of a Pandas DataFrame by the value of a column?

I have a Pandas DataFrame df, with two columns A and B. A is also the index.

B has a very small range of permissible values (in my case, B is a boolean). How do I quickly answer the query: "all rows in df for which the value of B is x?"

For example, "which rows of df have a B value that is True?"

For my particular usecase, since there's only two columns, just grouping the values in the A column would be enough, i.e. "which values of A have a corresponding B value that is x?".

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Here is an example using http://www.gregreda.com/2013/10/26/intro-to-pandas-data-structures/ also I recommend you going over that tutorial along with the pandas documentation.

>>> data = {'year': [2010, 2011, 2012, 2011, 2012, 2010, 2011, 2012],
...         'team': ['Bears', 'Bears', 'Bears', 'Packers', 'Packers', 'Lions', 'Lions', 'Lions'],
...         'wins': [11, 8, 10, 15, 11, 6, 10, 4],
...         'losses': [5, 8, 6, 1, 5, 10, 6, 12]}
>>> football = pd.DataFrame(data, columns=['year', 'team', 'wins', 'losses'])
>>> football
   year     team  wins  losses
0  2010    Bears    11       5
1  2011    Bears     8       8
2  2012    Bears    10       6
3  2011  Packers    15       1
4  2012  Packers    11       5
5  2010    Lions     6      10
6  2011    Lions    10       6
7  2012    Lions     4      12

This is what you wanted to do:

>>> football[football['team'] == 'Lions']
   year   team  wins  losses
5  2010  Lions     6      10
6  2011  Lions    10       6
7  2012  Lions     4      12

[3 rows x 4 columns]

In your case you need to replace these column headers and do what you want to obtain from the data frame.

df[df['B'] = True]

I gave above example so you can get more familiar with the operation and play around to get a good idea.


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

...