Skip to content

Instantly share code, notes, and snippets.

@turian
Created August 6, 2020 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save turian/f78ceafec6570805eb938ab66d30660a to your computer and use it in GitHub Desktop.
Save turian/f78ceafec6570805eb938ab66d30660a to your computer and use it in GitHub Desktop.
Pandas function to take to the first value for each of a column value
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
@turian
Copy link
Author

turian commented Aug 6, 2020

Then graphs can be plotted using

w = max_by_column(df, 'colname')
w.apply(pd.to_numeric, errors='ignore').plot.line(y='mean corr', x='colname', logx=True)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment