11
      id gender status dept  var1  var2  salary
0   P001      M     FT   DS   2.0   8.0     NaN
1   P002      F     PT   FS   3.0   NaN    54.0
2   P003      M    NaN  AWS   5.0   5.0    59.0
3   P004      F     FT  AWS   NaN   8.0   120.0
4   P005      M     PT   DS   7.0  11.0    58.0
5   P006      F     PT  NaN   1.0   NaN    75.0
6   P007      M     FT   FS   NaN   NaN     NaN
7   P008      F    NaN   FS  10.0   2.0   136.0
8   P009      M     PT  NaN  14.0   3.0    60.0
9   P010      F     FT   DS   NaN   7.0   125.0
10  P011      M    NaN  AWS   6.0   9.0     NaN

print(df.mean())

FutureWarning: Dropping of nuisance columns in DataFrame reductions (with 'numeric_only=None') is deprecated; in a future version this will raise TypeError.  Select only valid columns before calling the reduction.
  print(df.mean())

when corrected my code as :

print(df.mean(numeric_only=True))

I solve it , without an error.

Is there any other way to solve it ?

Lima
  • 141
  • 1
  • 1
  • 6

2 Answers2

17

No, there's no other way to solve it. Using numeric_only=True is the right way.

You're getting that warning because there are some columns that contain strings, but df.mean() only works with columns that contain numbers (floats, ints, nan, etc.).

Using numeric_only=True causes df.mean() to ignore columns that contain non-numbers, and only calculate the mean for columns that only contain numbers.

0

Try this:

import pandas as pd
pd.options.mode.chained_assignment = None
buddemat
  • 4,552
  • 14
  • 29
  • 49
я яя
  • 1
  • 2
  • 3
    It is not clear how this solves the problem. Please provide enough explanation and link to resources or additional documentation to support your answer. – Azhar Khan Feb 11 '23 at 07:19