dataframe - how to add specific row and columns from pandas -
i have data set below when tried sum column sum year. want sum jan dec.
the code tried isdata.sum(axis=0)
out[64]: year jan feb mar apr may jun jul aug sep oct nov dec 0 1981 32 26 62 22 23 98 80 163 13 122 144 109 1 1982 42 30 54 50 192 37 52 77 48 46 74 52 2 1983 78 10 107 46 36 105 80 25 188 58 36 78 3 1984 72 29 34 11 29 97 67 51 145 114 51 51 4 1985 83 37 42 69 23 25 104 50 76 53 74 63 5 1986 50 12 69 51 77 39 121 234 56 45 54 89 6 1987 23 28 22 4 55 77 92 122 65 34 54 24 7 1988 72 64 40 37 41 51 106 173 36 69 38 66 8 1989 9 28 51 55 38 42 11 68 23 74 55 59 9 1990 74 79 56 46 30 25 128 42 153 79 60 42 10 1991 59 29 41 24 78 142 54 124 71 27 47 37
filter columns first , sum
:
in [274]: df[df.columns[1:]].sum(axis=0) out[274]: jan 594 feb 372 mar 578 apr 415 may 622 jun 738 jul 895 aug 1129 sep 874 oct 721 nov 687 dec 670 dtype: int64
if want sum row-wise pass axis=1
:
in [291]: df['total'] = df[df.columns[1:]].sum(axis=1) df out[291]: year jan feb mar apr may jun jul aug sep oct nov dec total 0 1981 32 26 62 22 23 98 80 163 13 122 144 109 894 1 1982 42 30 54 50 192 37 52 77 48 46 74 52 754 2 1983 78 10 107 46 36 105 80 25 188 58 36 78 847 3 1984 72 29 34 11 29 97 67 51 145 114 51 51 751 4 1985 83 37 42 69 23 25 104 50 76 53 74 63 699 5 1986 50 12 69 51 77 39 121 234 56 45 54 89 897 6 1987 23 28 22 4 55 77 92 122 65 34 54 24 600 7 1988 72 64 40 37 41 51 106 173 36 69 38 66 793 8 1989 9 28 51 55 38 42 11 68 23 74 55 59 513 9 1990 74 79 56 46 30 25 128 42 153 79 60 42 814 10 1991 59 29 41 24 78 142 54 124 71 27 47 37 733
Comments
Post a Comment