【问题标题】:Finding the mean of nuisance columns in DataFrame error在 DataFrame 错误中查找有害列的平均值
【发布时间】:2023-02-09 06:23:18
【问题描述】:
      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())

将我的代码更正为:

print(df.mean(numeric_only=True))

我解决了,没有错误。

还有其他方法可以解决吗?

【问题讨论】:

  • 为什么以及你还想怎样解决它?
  • 这可能是由于在 pandas 的较新版本中使用了 numeric_only=None,它已被弃用。你的pandas版本是>=1.35吗?

标签: pandas mean


【解决方案1】:

不,没有其他方法可以解决它。使用numeric_only=True 是正确的方法。

您收到该警告是因为有些列包含字符串,但 df.mean() 仅适用于包含数字(浮点数、整数、nan 等)的列。

使用numeric_only=True 会导致df.mean() 忽略包含非数字的列,并且只计算仅包含数字的列的平均值。

【讨论】:

    【解决方案2】:

    尝试这个:

    import pandas as pd
    pd.options.mode.chained_assignment = None
    

    【讨论】:

      猜你喜欢
      • 2017-04-15
      • 2014-10-08
      • 2018-07-12
      • 2020-08-10
      • 2021-12-24
      • 2019-05-02
      • 1970-01-01
      • 2012-02-20
      相关资源
      最近更新 更多