I'm pretty sure there has to be a less twisted method, but this is the only thing I could think of:
Do a groupby()for the "idCDU" column
You make an .apply(funcion)envelope of the result. The function in question will receive as a parameter each of the sub-dataframes in which the main dataframe has been grouped, so within that function you can index by the "dateValue" column and use it .valor.idxmax()to return the index (which will then be the date) of the row that has the maximum value.
Although the idea is twisted, the implementation is a couple of lines:
# Leer dataframe
import pandas as pd
df = pd.read_csv("file_name.csv", sep=";", index_col=0)
# Implementación de la idea
def getmax(subdf):
return subdf.set_index("fechaValor").valor.idxmax()
df_max_fecha = df.groupby("idCDU").apply(getmax)
I'm pretty sure there has to be a less twisted method, but this is the only thing I could think of:
groupby()
for the "idCDU" column.apply(funcion)
envelope of the result. The function in question will receive as a parameter each of the sub-dataframes in which the main dataframe has been grouped, so within that function you can index by the "dateValue" column and use it.valor.idxmax()
to return the index (which will then be the date) of the row that has the maximum value.Although the idea is twisted, the implementation is a couple of lines:
The result
df_max_fecha
would come out: