假設我有一個數據幀,如:追加一個系列作爲行數據幀熊貓(Python的3.4)
df2 = pd.DataFrame({ 'A' : 1.,
'B' : pd.Timestamp('20130102'),
'C' : pd.Series(1,index=list(range(4)),dtype='float32'),
'D' : np.array([3] * 4,dtype='int32'),
'E' : pd.Categorical(["test","train","test","train"]), })
這看起來像
A B C D E
0 1 2013-01-02 1 3 test
1 1 2013-01-02 1 3 train
2 1 2013-01-02 1 3 test
3 1 2013-01-02 1 3 train
我想追加一個「彙總」行對於數字列,並將在「彙總」在列E.
所以我有什麼是:
totals=pd.Series('Total', index=['E'])
totals = df2.sum(numeric_only=True).append(totals)
這將產生
totals
A 4
C 4
D 12
E Total
dtype: object
所以,如果我嘗試
df2.append(totals, ignore_index=True)
我得到
A B C D E
0 1 2013-01-02 00:00:00 1 3 test
1 1 2013-01-02 00:00:00 1 3 train
2 1 2013-01-02 00:00:00 1 3 test
3 1 2013-01-02 00:00:00 1 3 train
4 4 NaN 4 12 NaN
我在這裏的問題是,爲什麼沒有列 'E' 有一個 「總計」,爲什麼它是NaN嗎?
它不適合我。 – jezrael