4

I have a following data frame. I need to find count of each type of MNTPCODE for each donor.

    CONTID  MEDIUMCODE  MNTOPCODE   CLASCODE    EXTRELNO    CONTDIREC   CONTDATE
   000405402     CI        CTS        CT         0000020       O           1994-05-09
    000430904   CI       CTS          CT         0000020        O       1994-06-14
   000592732    CI       CTS          CT        0000020         O       1994-10-21
Seema Mudgil
  • 365
  • 1
  • 7
  • 15

1 Answers1

6

Try DataFrame.groupby(index) with .size()

I added extra row with 'FAKECODE' for better understanding

df :

      CONTID MEDIUMCODE MNTOPCODE CLASCODE EXTRELNO CONTDIREC    CONTDATE
0  000405402         CI       CTS       CT  0000020         O  1994-05-09
1  000430904         CI       CTS       CT  0000020         O  1994-06-14
2  000592732         CI       CTS       CT  0000020         O  1994-10-21
3  000592733         CI  FAKECODE       CT  0000020         O  1994-10-21

then used df.groupby('MNTOPCODE').size() =>

MNTOPCODE
CTS         3
FAKECODE    1
dtype: int64
J-min
  • 457
  • 4
  • 9
  • Thanks! But i need to find out type of code for each donor. – Seema Mudgil Apr 23 '17 at 11:56
  • Could you specify details of your data? I can't understand what each column mean and what the 'code', 'donors' are. Give me a sample format you want. – J-min Apr 24 '17 at 12:14
  • here donor is the person who is donating to an NGO : donor-id is mentioned in column (EXTRELNO) . MNTOPCODE is the column which shows topic of communication. A donor can contact for different reasons. I need to know how many times a donor is contacting for each reason. – Seema Mudgil May 02 '17 at 05:51
  • i need result in following format. `EXTRELNO MNTOPCODE COUNT` – Seema Mudgil May 02 '17 at 05:57