Displaying unique name with total of column value in a group with additional variables in python
<p>I'm learning Python and thought working on a project might be the best way to learn it. I have about 200,000 rows of data in which the data shows list of medication for the patient. Here's a sample of the data. </p>

<pre><code>PTID PTNAME     MME   DRNAME       DRUGNAME                    SPLY STR QTY  FACTOR
1   PATIENT, A  2700    DR, A   OXYCODONE HCL 15 MG             30  15  120 1.5
1   PATIENT, A  2700    DR, B   OXYCODONE HCL 15 MG             30  15  120 1.5
2   PATIENT, B  4050    DR, C   MORPHINE SULFATE ER 15 MG       30  15  270 1
2   PATIENT, B  4050    DR, C   MORPHINE SULFATE ER 15 MG       30  15  270 1
2   PATIENT, B   840    DR, A   OXYCODONE-ACETAMINOPHE 10MG-32  14  10  56  1.5
2   PATIENT, B  1350    DR, C   OXYCODONE-ACETAMINOPHE 5 MG-32  15  5   180 1.5
3   PATIENT, C  1350    DR, C   OXYCODONE-ACETAMINOPHE 5 MG-32  15  5   180 1.5
3   PATIENT, C  1800    DR, D   OXYCODONE-ACETAMINOPHE 10MG-32  30  10  120 1.5
</code></pre>

<p>I've been thinking about this a lot and have tried many ways but none of the code produce any results or makes any sense. Honestly, I don't even know where to begin. A little help would be highly appreciated. </p>

<p>So, what I want to do is consolidate the data for each patients and calculate the <code>Total MME</code> for each patient. The <code>DRUGNAME</code> should show the one that has higher MME. In other words, the dataframe should only have one row for each patient.  </p>

<p>One thing I did try is </p>

<pre><code>groupby_ptname = semp.groupby('PTNAME').apply(lambda x: x.MME.sum())
</code></pre>

<p>which shows unique patient names with total MME,  but I'm not sure how to add other variables in this new dataframe. </p>

<p>Have another look at the documentation for the <a href="http://pandas.pydata.org/pandas-docs/stable/groupby.html" rel="nofollow">pandas groupby methods</a>. </p>

<p>Here's something that could work for you:</p>

<pre><code>#first get the total MME for each patient and drug combination
total_mme=semp.groupby(['PTNAME','DRUGNAME'])['MME'].sum()
#this will be a series object with index corresponding to PTNAME and DRUGNAME and values corresponding to the total MME
#now get the indices corresponding to the drug with the max MME total
max_drug_indices=total_mme.groupby(level='PTNAME').idxmax()
#index the total MME with these indices
out=total_mme[max_drug_indices]
</code></pre>

python pandas