I have a Dataframe like:
import pandas as pd
from datetime import datetime
data = {'PRACTICE': [1,2,3,1,1], 'Postcode': ['BT1234', 'BT4321', 'AB1234', 'BT1234', 'BT1234'],
'month': [datetime(2013, 4, 1), datetime(2013, 4, 1), datetime(2013, 4, 1), datetime(2013, 3, 1), datetime(2013, 3, 1)],
'VTN_NM': ['Gabapentin', 'Gabapentin', 'Diazepam', 'Diazepam', 'Gabapentin elixir'],
'Total Items': [6, 5, 11, 4, 3]}
df = pd.DataFrame(data)
and a list of search terms for the VTM_NM column:
search_terms = [
'Gabapentin', 'Pregabalin', 'Tramadol', 'Oxycodone',
'Morphine', 'Diazepam', 'Temazepam', 'Codeine',
'Buprenorphine', 'Methadone', 'Methylphenidate'
]
I want to count/sum the Total Items column for all the search terms when list items are present in the VTM_NM column, based on the month, PRACTICE and postcode columns. So when any of the items in the list are present in the VTM_NM column it adds the Total Quantity for that given month practice and postcode value. The counted values could then be stored in a new column e.g. gabapentin_count etc. If gabapentin was prescribed 5 times in one month by a practice of a given postcode then the total count for each of the 5 prescriptions would be added.
So for this input, the output should look like:
| PRACTICE | Postcode | Month | Gaba_count | Diaz_count |
|---|---|---|---|---|
| 1 | BT1234 | 2013.3 | 3 | 4 |
| 1 | BT1234 | 2013.4 | 6 | 0 |
| 2 | BT4321 | 2013.4 | 5 | 0 |
| 3 | AB1234 | 2013.4 | 0 | 11 |
| etc |
I think I need to use groupby() to solve this problem, but I can't figure it out, and none of the code I found online works either. How can I get this result?