假设我有一个数据帧,如:追加一个系列作为行数据帧熊猫(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