i have dataframe in pandas below. trying add row row-name ="total", sum of each column. using following code : df.loc["total"] = df.sum(axis =1)
i getting nan sum of each column. idea why , how solve ?
use:
df.loc["total"] = df.sum()
or if need convert first string values of columns float:
df.loc["total"] = df.astype(float).sum()
sample:
df = pd.dataframe({'a':[1,2,3], 'b':[4,5,6], 'c':[7,8,9], 'd':[1,3,5], 'e':[5,3,6], 'f':[7,4,3]}) print (df) b c d e f 0 1 4 7 1 5 7 1 2 5 8 3 3 4 2 3 6 9 5 6 3 df.loc["total"] = df.sum() print (df) b c d e f 0 1 4 7 1 5 7 1 2 5 8 3 3 4 2 3 6 9 5 6 3 total 6 15 24 9 14 14
Comments
Post a Comment