Python programming, with examples in hydraulic engineering and in hydrology.
Sunday, November 26, 2017
Pandas sum column values according to another columns value
One-liner code to sum Pandas second columns according to same values in the first column.
df2 = df1.groupby(df1.columns[0])[df1.columns[1]].sum().reset_index()
For example, applying to a table listing pipe diameters and lenghts, the command will return total lenghts according to each unique diameters.
This functionality is similar to excel's pivot table sum.
Labels:
column,
one liner,
pandas,
pivot table,
python,
series,
timeseries
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment