Good morning, could you help me with...
I have the Following DataFrame
fecha Medicamento Dosis ClaseServicio
0 2022-11-10 Vancomicina IV 4000.00 HOSPITALIZACION
1 2022-11-12 Vancomicina IV 2.00 HOSPITALIZACION
2 2022-11-01 Ceftriaxona IV 10.00 HOSPITALIZACION
3 2022-11-10 Ceftriaxona IV 10.00 HOSPITALIZACION
4 2022-11-10 Ertapenem IV 20.00 HOSPITALIZACION
5 2022-11-10 Ertapenem IV 20.00 HOSPITALIZACION
6 2022-11-10 Cefepime IV 9.00 CUIDADO CRITICO
7 2022-11-10 Cefepime IV 9.00 CUIDADO CRITICO
8 2022-11-10 Meropenem IV 30.00 HOSPITALIZACION
9 2022-11-10 Meropenem IV 15.00 CUIDADO CRITICO
10 2022-11-10 Piperacilna/tazobactam IVIV 3.00 CUIDADO CRITICO
First it should, Separate the rows by type of value Column "ClassService" , which are "HOSPITALIZATION" or "CRITICAL CARE", Second it should, it should add the Dose Column "Dose" of each type of Medication of Column "Medication"
All this to have the following output. I understand that some steps may be unnecessary Example:
This for each month you have.
I have tried in the following way:
CrossTab function
pd.crosstab(df_filtro.PAV ,[ df_filtro.ClaseServicio], aggfunc = "sum", values = df_filtro.Dosis)
pd.crosstab([df_filtro.fecha,df_filtro.PAV ],[ df_filtro.ClaseServicio], aggfunc = "sum", values = df_filtro.Dosis)
But I don't know how to solve the last thing that shows me per month the sum of each PAV/Medication.
I'd appreciate your help. Cheers
Good day,
You can achieve this by using
pandas.DataFrame.groupby
together withpandas.DataFrame.unstack
Using the following
dataframe
from the "sample2.csv" file as an example:Note: I added the same data but for December so that the result can be seen correctly.
You have to group
dataframe
by type of "Medication", then by month and by "Class of Service". But since you are interested in displaying the name of the month, we usepandas.Series.dt.month_name
localees
to display the names in Spanish.Note: As a prerequisite, the "date" column must be of type
datetime64[ns]
.Since the was grouped, you
dataframe
need the sum of "Dose", so the previous line would look like this:Which would return the following
dataframe
:Note: Both the indices and the columns go from 0 to
n
, so the "Medication" column would have the index 0, "date" the index 1 and "ClassService the index 2.We use
unstack(1)
so that the group of months becomes columns, now the multi index would be left with 2 elements that would be "Medication" and "ClassService", we use again `unstack(1) so that the group of "ClassService" (Which now has index 1) becomes a second "group" of columns.Full example:
This prints the following
dataframe
: