我想計算每個 ID 的平均速度,我使用了這個代碼
df_Speed=df2.groupby('ID').agg(Total_Speed=('speed Km/h', 'sum'),Total_steps=('ID', 'count')).reset_index()
df_Speed['Avg_Speed']=df_Speed['Total_Speed']/df_Speed['Total_steps']
df_Speed.head()
但我得到資訊作為速度!
ID Total_Speed Total_schritte Avg_Speed
1817603 2199.422386 149 14.761224
1817615 inf 1178 inf
1817679 inf 452 inf
1817888 5436.540730 271 20.061036
我怎樣才能獲得速度而不是這個inf
uj5u.com熱心網友回復:
您正在閱讀資料,它以某種方式創造了無窮大。如您所知,無窮大除以某個常數仍然是無窮大。
要解決此問題,您可能需要調整資料,或用平均值填充無窮大。
import numpy as np
# Replace inf with nan
df['Total_Speed'].replace([np.inf, -np.inf], np.nan, inplace=True)
# Replace nan with mean
df['Total_Speed'].fillna(df['Total_Speed'].mean())
來源:從熊貓的資料框中洗掉無限值?
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/358220.html
上一篇:將識別符號添加到資料幀索引
下一篇:用Pandas列映射字典
