简体   繁体   中英

How to correctly group columns?

I have a Data Frame with this columns:

DF.head():
Email           Month    Year
abc@Mail.com      1      2018
abb@Mail.com      1      2018
abd@Mail.com      2      2019
     .   
     .
abbb@Mail.com     6      2019

What I want to do is to get the total of email adresses in each month for both years 2018 and 2019 (knowing that I don't need to filter, since I have only this two years).

This is what I've done, but I want to make sure that this is right:

 Stats = DF.groupby(['Year','Month'])['Email'].count()

Any Suggestion?

It depends what need.

If need exclude missing values or missing values not exist in Email column, your solution is right, use GroupBy.count :

Stats = DF.groupby(['Year','Month'])['Email'].count()

If need count all groups also with missing values (if exist) use GroupBy.size :

Stats = DF.groupby(['Year','Month']).size()

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