Pandas functions
Pandas Sum β pd.DataFrame.sum()
Sum across rows or columns in pandas
Ah, sum. The backbone of any good mathematical operation. Pandas sum gives you the power to sum entire rows or columns.
You can use this function on a DataFrame or a Series.
I use Pandas Sum for series addition mostly. Especially when counting the number of βTrueβ entries when filtering my rows.
Pseudo Code: With your DataFrame, the summation of your rows or columns.
Pandas Sum
Pandas Sum β How to sum across rows or columns in pandas dataframe
Sum Parameters
Sum has simple parameters. 90% of the time youβll just be using βaxisβ but itβs worth learning a few more.
- axis β Axis to sum on. The way I remember this is to sum across rows set axis=0, to sum across columns set axis=1.
- skipna (Default: True) β If your dataset has NA/null values, then you may want to skip βem
- level (Default: None) β In case you have a multi index, you can specify which part of that multi index you want to sum across. Check out an example below.
- numeric_only (Default: None) β This means youβll only include float, int, and booleans. Youβll find that sometimes your column will have mix datatypes (strings, functions, etc.). This parameter will help you skip those when summing.
- min_count (Default: 0) β The minimum number of entries to in order to compute a sum. If the number of entries is below your minimum, then .sum() will return an NA.
Now the fun part, letβs take a look at a code sample