第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

為什么 pd.concat 將結(jié)果類型從 int 更改為 object?

為什么 pd.concat 將結(jié)果類型從 int 更改為 object?

回首憶惘然 2021-09-24 15:20:25
我正在用 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)到我的代碼中并且它起作用了。


查看完整回答
反對 回復(fù) 2021-09-24
  • 2 回答
  • 0 關(guān)注
  • 552 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號