标签: python python-2.7 python-3.x pandas dataframe
当我使用df['mean'] = df.mean(axis = 1)时,它总是添加额外的每行平均COLUMN。但现在我需要一个额外的行来获得每个COLUMNS'意思。所以我切换到df['mean'] = df.mean(axis = 0),但它仍然有一个额外的列,但所有NaN。如何获得每列的行?平均?
df['mean'] = df.mean(axis = 1)
df['mean'] = df.mean(axis = 0)
以下是↓
答案 0 :(得分:1)
IIUC
df = pd.DataFrame(np.arange(16).reshape(-1, 4), list('abcd'), list('ABCD')) df.loc['mean', :] = df.mean(0) df.loc[:, 'Mean'] = df.mean(1) df.loc['mean', 'Mean'] = np.nan df