0

I have a pandas dataframe & want to calculate the average of some certain columns. But, there are some nan values & my lambda function is ignoring the rows with a missing value in any column. What to do? Here is my code.

avgGDP = fdata.apply(lambda x: np.average(x[rows]), axis=1)
Adam Boinet
  • 521
  • 8
  • 22
Seam Ahmed
  • 21
  • 2
  • Please use pandas built-in function mean: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.mean.html . It allows you to decide on what to do with nan values – Artyom Akselrod Jun 03 '20 at 09:19
  • Use `np.nanmean` as explained in [this answer](https://stackoverflow.com/a/35832403/2641825) – Paul Rougieux Jun 03 '20 at 12:33
  • Does this answer your question? [NumPy: calculate averages with NaNs removed](https://stackoverflow.com/questions/5480694/numpy-calculate-averages-with-nans-removed) – Paul Rougieux Jun 03 '20 at 12:33

0 Answers0