Created
August 6, 2020 18:44
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Then graphs can be plotted using