def first_by_column(df, colname): """ Pandas function to take to the first value for each of a column value. Useful if the table if sorted in descending order and you want to group over a particular column, to find the max for each value of that field. """ newdf = [] #print(colname) #print(df) colvals = pd.unique(df.loc[:, [colname]].squeeze()) #print(colvals) for f in colvals: newdf.append(df.loc[df[colname] == f].iloc[0,:]) return pd.concat(newdf, axis=1).T