我正在用 Pandas 解析幾個(gè) csv 文件并將它們連接成一個(gè)大數(shù)據(jù)幀。然后,我想groupby計(jì)算mean().這是一個(gè)示例數(shù)據(jù)框:df1.head() Time Node Packets0 1 0 02 1 1 04 1 2 06 1 3 08 1 4 0df1.info(verbose=True)<class 'pandas.core.frame.DataFrame'>Int64Index: 27972 entries, 0 to 55942Data columns (total 3 columns):Time 27972 non-null int64Node 27972 non-null int64Packets 27972 non-null int64dtypes: int64(3)memory usage: 874.1 KBNone然后我將它們連接起來(為了簡單起見,三個(gè)數(shù)據(jù)幀)df_total = pd.concat([df1, df2, df3])df_total.info(verbose=True) 結(jié)果是<class 'pandas.core.frame.DataFrame'>Int64Index: 83916 entries, 0 to 55942Data columns (total 3 columns):Time 83916 non-null objectNode 83916 non-null objectPackets 83916 non-null objectdtypes: object(3)memory usage: 2.6+ MBNone最后,我嘗試:df_total = df_total.groupby(['Time'])['Packets'].mean()這就是錯(cuò)誤pandas.core.base.DataError: No numeric types to aggregate出現(xiàn)的地方。雖然我從其他職位如明白這是熊貓改變dtype因?yàn)閚on-null,我無法解決提出的解決方案我的問題。我該如何解決?
2 回答

翻閱古今
TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超5個(gè)贊
我發(fā)現(xiàn)另一篇文章提到數(shù)據(jù)幀必須用 dtype 初始化,否則它們是對象類型
Did you initialize an empty DataFrame first and then filled it? If so that's probably
why it changed with the new version as before 0.9 empty DataFrames were initialized
to float type but now they are of object type. If so you can change the
initialization to DataFrame(dtype=float).
所以我添加df_total = pd.DataFrame(columns=['Time', 'Node', 'Packets'], dtype=int)到我的代碼中并且它起作用了。
添加回答
舉報(bào)
0/150
提交
取消