Skip to content

Instantly share code, notes, and snippets.

@janbenetka
Created October 3, 2020 22:25
Show Gist options
  • Save janbenetka/5867e427b62afb1b1e1669a4324efbcf to your computer and use it in GitHub Desktop.
Save janbenetka/5867e427b62afb1b1e1669a4324efbcf to your computer and use it in GitHub Desktop.
[Total sum row and column in Pandas] Add total row and coumn in Pandas dataframe #pandas #dataframes
df = pd.DataFrame(dict(A=[2,6,3],
B=[2,2,6],
C=[3,2,3]))
df['col_total'] = df.apply(lambda x: x.sum(), axis=1)
df.loc['row_total'] = df.apply(lambda x: x.sum())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment