【问题标题】:Error while trying to run ```corr()``` in python with pandas module尝试使用 pandas 模块在 python 中运行 ```corr()``` 时出错
【发布时间】:2022-11-15 17:31:37
【问题描述】:

尝试使用 pandas 模块在 python 中运行 corr() 方法时,出现以下错误:

FutureWarning: The default value of numeric_only in DataFrame.corr is deprecated. In a future version, it will default to False. Select only valid columns or specify the value of numeric_only to silence this warning.
  print(df.corr())

笔记(只是为了澄清):- df 是从 csvfile 读取的数据帧的名称。

例如:-

import pandas as pd

df = pd.read_csv('Data.csv')
print(df.corr())

问题只要在于引发上述错误的corr() 方法:

FutureWarning: The default value of numeric_only in DataFrame.corr is deprecated. In a future version, it will default to False. Select only valid columns or specify the value of numeric_only to silence this warning.

我部分理解该错误,但我想知道:

是否有任何其他替代方法可以执行corr() 的相同功能来识别数据集中每一列之间的关系?就像有没有一种方法可以在不使用的情况下复制该功能 corr() 方法?

抱歉,如果我的问题无论如何是错误的或不恰当的,我愿意接受反馈。

提前致谢。

【问题讨论】:

  • 您还有其他原因要避免使用corr 方法吗?因为它一般不被弃用,只有其numeric_only 参数的默认值。使用df.corr(numeric_only = True)(或False,取决于需要)应该可以消除警告。
  • 好的,谢谢...除了在我的代码中引发错误之外,我没有任何其他理由避免它。
  • 您应该编辑帖子并添加代码以获得更好的帮助。
  • 问题不在于我的代码,而在于一种方法 - corr 方法
  • 但是@matszwecja 的评论解决了我的问题..我想结束这个问题。

标签: python pandas future-warning


【解决方案1】:

问题仅在于未弃用的一个功能corr() 但它的numeric_only 函数中的参数是。所以,df.corr(numeric_only = *[True/False]*) 可以根据需要设置为 false 或 true。 您可以通过documentation 了解更多信息。

p.s - 我写是为了更容易理解,而且这个问题对我来说是众所周知的。

【讨论】:

    【解决方案2】:

    感谢@matszwecja 的回答,使用df.corr(numeric_only = True)(或False,取决于需要)应该可以消除警告,

    仅不推荐使用 numeric_only 的默认值,即在未来版本中将其设置为 false:pandas documentation/reference

    P.S:-我写了这个答案来结束这个问题,因为它已经在 cmets 中得到了回答。

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。
    猜你喜欢
    • 2020-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-02
    • 1970-01-01
    • 2014-06-06
    • 2022-12-07
    • 1970-01-01
    相关资源
    最近更新 更多