Target
I have a Pandas data frame, as shown below, with multiple columns and would like to get the total of column, MyColumn
.
Data Frame - df
:
print df
X MyColumn Y Z
0 A 84 13.0 69.0
1 B 76 77.0 127.0
2 C 28 69.0 16.0
3 D 28 28.0 31.0
4 E 19 20.0 85.0
5 F 84 193.0 70.0
My attempt:
I have attempted to get the sum of the column using groupby
and .sum()
:
Total = df.groupby['MyColumn'].sum()
print Total
This causes the following error:
TypeError: 'instancemethod' object has no attribute '__getitem__'
Expected Output
I'd have expected the output to be as followed:
319
Or alternatively, I would like df
to be edited with a new row
entitled TOTAL
containing the total:
X MyColumn Y Z
0 A 84 13.0 69.0
1 B 76 77.0 127.0
2 C 28 69.0 16.0
3 D 28 28.0 31.0
4 E 19 20.0 85.0
5 F 84 193.0 70.0
TOTAL 319
Similar to getting the length of a dataframe,
len(df)
, the following worked for pandas and blaze:or alternatively
Another option you can go with here:
You can also use
append()
method:Update:
In case you need to append sum for all numeric columns, you can do one of the followings:
Use
append
to do this in a functional manner (doesn't change the original data frame):Use
loc
to mutate data frame in place:As other option, you can do something like below
Below script, you can use for above data
You should use
sum
:Then you use
loc
withSeries
, in that case the index should be set as the same as the specific column you need to sum:because if you pass scalar, the values of all rows will be filled:
Two other solutions are with
at
, andix
see the applications below:Note: Since Pandas v0.20,
ix
has been deprecated. Useloc
oriloc
instead.If there is any issue in this the please correct me..