-1

I have a dataframe with multiple columns and I want to calculate some averages, however the result is often an inf value, which is messing up my dataframe. Is it possible to round down the inf value in Python?

Some example of code:

df['Htgs/2'] = df.groupby('Home Team', ) ['Hg'].rolling(window=a, min_periods=a ).mean().reset_index(0,drop=True)
df['Htgs/2']= (((df['Htgs/2'] * a) - df['Hg']) / (a-1)) / df['Lha']
df['Agcr/2'] = df.groupby('Away Team', ) ['Agcr'].rolling(window=a, min_periods=a ).mean().reset_index(0,drop=True)
df['Agsr/2']= (((df['Agcr/2'] * a) - df['Agcr']) / (a-1))
df = df.round(decimals=3)
Jason K Lai
  • 1,500
  • 5
  • 15

2 Answers2

0

I don't know how well it would work in your use-case overall, but you can replace the inf values with an arbitrarily large number:

df_new = df.replace( [ inf, -inf ], 1e99 )
print( df_new )

Without knowing your exact dataframe, this is only a rough example. You're going to have to modify it to fit your data and criteria.

Jason K Lai
  • 1,500
  • 5
  • 15
0

you could do this to just not consider the inf

df[df['your_column'] != np.inf].mean()
Derek Eden
  • 4,403
  • 3
  • 18
  • 31