简体   繁体   中英

Use of index in pandas DataFrame for groupby and aggregation

I want to aggregate a single column DataFrame and count the number of elements. However, I always end up with an empty DataFrame:

pd.DataFrame({"A":[1, 2, 3, 4, 5, 5, 5]}).groupby("A").count()

Out[46]: 
Empty DataFrame
Columns: []
Index: [1, 2, 3, 4, 5]

If I add a second column, I get the desired result:

pd.DataFrame({"A":[1, 2, 3, 4, 5, 5, 5], "B":[1, 2, 3, 4, 5, 5, 5]}).groupby("A").count()

Out[45]: 
   B
A   
1  1
2  1
3  1
4  1
5  3

Can you explain the reason for this?

Give this a shot:

import pandas as pd
print(pd.DataFrame({"A":[1, 2, 3, 4, 5, 5, 5]}).groupby("A")["A"].count())

prints

A
1    1
2    1
3    1
4    1
5    3

You have to add the grouped by column in your result:

import pandas as pd
pd.DataFrame({"A":[1, 2, 3, 4, 5, 5, 5]}).groupby("A").A.count()

Output:

A
1    1
2    1
3    1
4    1
5    3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM